[英]How to launch splash screen activity only when app is launched afresh?
I have an splash activity which should appear for a brief period of time before the home activity launches. 我有一个启动活动,应在家庭活动启动之前短暂出现。 This is my code:
这是我的代码:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class SplashActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
/****** Create Thread that will sleep for 5 seconds *************/
Thread background = new Thread() {
public void run() {
try {
// Thread will sleep for 2.5 seconds
sleep(2500);
//Remove activity
finish();
// After 2.5 seconds redirect to another intent
Intent i=new Intent(getBaseContext(),HomeActivity.class);
startActivity(i);
} catch (Exception e) {
}
}
};
// start thread
background.start();
}
@Override
protected void onDestroy() {
super.onDestroy();
}
} }
Currently this works perfectly except for the fact that when I go back to home screen (without explicitly killing the app), and resume it, it shows the splash screen again. 目前,除了我返回主屏幕(没有明确杀死该应用程序)并恢复运行,它再次显示启动屏幕这一事实外,它的运行情况非常理想。 How do I ensure the splash screen appears only when I launch the app afresh?
如何确保仅在重新启动应用程序时才显示启动屏幕?
Use noHistory and excludeFromRecent attribute in your manifest. 在清单中使用noHistory和excludeFromRecent属性。 In addition add the flags
Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP
另外,添加标志
Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP
Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP
to the intent used to start your real activity. Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP
用于启动您的真实活动的意图。
Edit: put android:launchMode="singleTop"
in your real main activity and use the intent flag (in combination of the others) FLAG_ACTIVITY_NEW_TASK
. 编辑:将
android:launchMode="singleTop"
放入您的实际主要活动中,并使用intent标志(结合其他标志) FLAG_ACTIVITY_NEW_TASK
。
I suggest you to save a preference containing the current timestamp in your SplashActivity's onCreate()
method. 我建议您在SplashActivity的
onCreate()
方法中保存一个包含当前时间戳的首选项。 The next time you open the app, check if the difference between the saved timestamp and the current timestamp is greater than an arbitrary value (a sort of grace period ) and decide to show/avoid the splash screen. 下次打开应用程序时, 请检查保存的时间戳和当前时间戳之间的差是否大于任意值 (某种宽限期 ),并决定显示/避免启动屏幕。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.