基本上我在两个活动之间进行了可运行的切换。我在onCreate runnable中有一个计时器,在主活动中设置为0毫秒,立即切换到启动画面。启动画面只是一个图像视图,然后使用类似的可运行程序在3000毫秒后立即切换回来。
我的问题是这样的;我可以简化主要活动的代码,如果我想立即加载SplashScreen.activity,我真的需要.postdelayed吗?
如果没有必要延迟,我将如何正确摆脱它,以便应用程序立即加载启动画面?
主要活动:
/*
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
然后是SplashScreen:
@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
答案 0 :(得分:0)
首先,永远不要使用匿名处理程序。使用处理程序对象。
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);
在摧毁
@Override
protected void onDestroy() {
super.onDestroy();
handler.removeCallbacks(runnable);
}
如果用户直接从任务管理器关闭应用程序,这将阻止应用程序崩溃。
OR
你应该使用
runOnUiThread(new Runnable() {
@Override
public void run() {
//Do your stuff here.
}
});
希望这有帮助。