本质上,我在两个活动之间有一个可运行的切换。我有一个计时器在OnCreate运行中,该计时器设置为主活动中的0毫秒,该计时器立即切换到Splash屏幕。Splash屏幕只是一个图像视图,然后在3000毫秒后使用类似运行的可运行。
向后切换。我的问题是这个;我可以简化主要活动上的代码吗?
如果不需要延迟,我将如何正确地摆脱它,以便该应用立即加载splashscreen?
主要活动:
/*
SPLASH SCREEN
*/
splashScreenRun = settings.getBoolean("splashScreenRun", splashScreenRun);
if (splashScreenRun == true) {
settings.edit().putBoolean("splashScreenRun", false).commit();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent splashIntent = new Intent(MainActivity.this, SplashActivity.class);
startActivity(splashIntent);
finish();
}
},0);
}
else {
settings.edit().putBoolean("splashScreenRun", true).commit();
}
//END
然后溅起屏幕:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
//splash screen
new Handler().postDelayed(new Runnable(){
@Override
public void run(){
Intent splashEndIntent = new Intent(SplashActivity.this, MainActivity.class);
startActivity(splashEndIntent);
finish();
}
},splashTimeout);
//end splash screen
首先从未使用匿名处理程序。使用处理程序对象。
Handler handler = new Handler();
runnable = new Runnable() {
@Override
public void run() {
startActivity(new
Intent(SplashActivity.this, MainActivity.class));
overridePendingTransition(R.anim.right_in, R.anim.right_out);
finish();
}};
handler.postDelayed(runnable, 3000);
和on Destroy
@Override
protected void onDestroy() {
super.onDestroy();
handler.removeCallbacks(runnable);
}
如果用户直接从任务管理器关闭应用程序,则将阻止应用程序崩溃。
或
您应该使用
runOnUiThread(new Runnable() {
@Override
public void run() {
//Do your stuff here.
}
});
希望这会有所帮助。