我的应用程序运行良好,但现在它再次运行一秒钟后再运行 第一:显示splashactivity然后登录活动
这是我的项目代码:
清单:
<application
android:name=".Adjust.GlobalApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:largeHeap="true"
android:launchMode="singleTask"
android:screenOrientation="portrait"
android:supportsRtl="false"
android:theme="@style/AppTheme.NoActionBar">
<activity
android:name=".Activity.SplashActivity"
android:theme="@style/Transparet">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Activity.LoginActivity"
android:label="@string/title_activity_login"
android:screenOrientation="portrait" />
<activity
android:name=".Activity.GetActiveCodeActivity"
android:screenOrientation="portrait" />
<meta-data
android:name="firebase_messaging_auto_init_enabled"
android:value="false" />
<meta-data android:name="com.onesignal.NotificationOpened.DEFAULT" android:value="DISABLE" />
<service
android:name=".Services.RestService"
android:screenOrientation="portrait" />
<activity
android:name=".Activity.WalletRechargeActivity"
android:screenOrientation="portrait" />
<activity
android:name=".Activity.MainActivity"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustPan" />
<activity
android:name=".Activity.BillsActivity"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustPan" />
<activity
android:name=".Activity.TransferActivity"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustResize|stateHidden" />
<activity
android:name=".Activity.BuyChargeActivity"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustPan" />
<activity
android:name=".Activity.TransferConfirmationActivity"
android:screenOrientation="portrait" />
<activity
android:name=".Activity.RegisterEventActivity"
android:screenOrientation="portrait" />
<activity android:name="com.soundcloud.android.crop.CropImageActivity" />
<activity
android:name=".Activity.CharityActivity"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustResize|stateHidden" />
<activity
android:name=".Activity.AddGroupTransferActivity"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateAlwaysVisible" />
<activity
android:name=".Activity.EditProfileActivity"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateHidden|adjustResize" />
<activity android:name=".Activity.ScanQrActivity" />
<activity android:name=".Activity.InvitationActivity" />
<activity
android:name=".Activity.BuyInternetActivity"
android:windowSoftInputMode="adjustPan" />
<activity android:name=".Activity.SetPayPassActivity" />
<activity android:name=".Activity.PaymentResult">
<intent-filter>
<action android:name="android.intent.action." />
<data
android:host="example"
android:scheme="example" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
</intent-filter>
</activity>
<activity android:name=".Activity.UpdateActvity" />
<activity android:name=".Activity.WebView" />
<activity android:name=".Activity.MagicMessage" />
<activity android:name=".Activity.ContactActivity"></activity>
</application>
SplashActivity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(getResources().getColor(R.color.status));
}
linearLayout_init = findViewById(R.id.linearLayout_init);
//animate login view
linearLayout_init.setVisibility(View.GONE);
Animation animTranslate = AnimationUtils.loadAnimation(SplashActivity.this, R.anim.translate);
animTranslate.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation arg0) { }
@Override
public void onAnimationRepeat(Animation arg0) { }
@Override
public void onAnimationEnd(Animation arg0) {
linearLayout_init.setVisibility(View.VISIBLE);
Animation animFade = AnimationUtils.loadAnimation(SplashActivity.this, R.anim.fade);
linearLayout_init.startAnimation(animFade);
}
});
ImageView imgLogo = findViewById(R.id.img_kipologo);
imgLogo.startAnimation(animTranslate);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
getSharedPreference();
}
}, 3000);
}
public void getSharedPreference(){
SharedPreferences sharedPreferences = getSharedPreferences("example", MODE_PRIVATE);
String isLogin = sharedPreferences.getString("mobileN", null);
if(isLogin != null )
{
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
startActivity(intent);
SplashActivity.this.finish();
} else {
Intent myIntent;
myIntent = new Intent(SplashActivity.this, LoginActivity.class);
startActivity(myIntent);
SplashActivity.this.finish();
}
}
我正在使用Preference,因为如果用户登录befor app则没有 再次登录活动
在我的调试中,似乎SpalshActivity运行很多次,我不知道为什么!
答案 0 :(得分:0)
在这个帖子中,
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
getSharedPreference();
}
}, 3000);
}
<强>问题:强>
您目前正在以毫秒为单位延迟3000.执行getSharedPreference()
方法后,您的应用程序将通过使用intent继续执行以下活动。但是,与此同时,你的splashScreen Activity正试图在前台保持其状态,因为它在执行任何操作之前都会强行等待3秒,这就是为什么你最终使用SplashScreen Activity调用/显示不止一个。
<强>修正:强>
解决这个问题的一种方法是没有任何延迟,只需在没有计时器的线程中执行你的getSharedPreference()方法。
例如:您可以使用:
Runnable runnable = new Runnable() {
@Override
public void run() {
//Do work here
}
};
new Thread(runnable).start();
旁注:
您无需使用SplashActivity.this.finish();
强制完成SplashScreen活动。必要时,活动将自行销毁。只有当你遇到内存问题而你想立即释放一些空格时,这个例子并非如此。