繁体   English   中英

仅在重新启动应用程序时如何启动启动屏幕活动?

[英]How to launch splash screen activity only when app is launched afresh?

我有一个启动活动,应在家庭活动启动之前短暂出现。 这是我的代码:

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();

  }

}

目前,除了我返回主屏幕(没有明确杀死该应用程序)并恢复运行,它再次显示启动屏幕这一事实外,它的运行情况非常理想。 如何确保仅在重新启动应用程序时才显示启动屏幕?

在清单中使用noHistory和excludeFromRecent属性。 另外,添加标志Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP用于启动您的真实活动的意图。

编辑:将android:launchMode="singleTop"放入您的实际主要活动中,并使用intent标志(结合其他标志) FLAG_ACTIVITY_NEW_TASK

我建议您在SplashActivity的onCreate()方法中保存一个包含当前时间戳的首选项。 下次打开应用程序时, 请检查保存的时间戳和当前时间戳之间的差是否大于任意值 (某种宽限期 ),并决定显示/避免启动屏幕。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM