简体   繁体   English

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

[英]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.

相关问题 仅在应用程序启动时显示启动画面,而不是每次调用包含启动画面代码的活动时显示启动画面 - Display splash screen only when the app is launched and not every time when the activity containing splash screen code, is called 如何在首次启动应用时启动具有特定活动的应用? - How to launch the app with a specific Activity only when the app is launched for the first time? 仅当应用程序被终止或首次启动时,如何才能启动启动屏幕? - How can I launch the splash screen only when the app is killed or started for the first time ? 仅在首次启动时如何具有启动屏幕? - How to have splash screen at first launch only? 如何避免在后台运行时启动应用程序时显示启动画面? - How to avoid showing splash screen when app is launched while running in background? 退出应用程序时出现启动屏幕活动 - splash screen activity appears when exiting app 启动 Qt/QML android 应用程序时如何立即显示启动画面? - How to display the splash screen immediately when Qt/QML android app is launched? 如何在应用程序启动“新鲜”时显示启动画面? - How to show splash screen only when the app starts “fresh”? 如何始终从启动屏幕启动应用程序,然后最后启动活动 - How to always launch application from splash screen then last left activity 如何仅在活动启动时才显示启动画面而不是在恢复活动时? - how do i show a splash screen only when the activity starts up not when it is resumed?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM