【问题标题】:How can I launch the splash screen only when the app is killed or started for the first time ?仅当应用程序被杀死或首次启动时,如何才能启动启动画面?
【发布时间】:2015-06-30 09:15:46
【问题描述】:

我不知道为什么,但启动画面总是启动。我希望它仅在应用程序被终止或第一次运行时启动。

这是我的代码:

public class SplashScreenActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_splash_screen);

        Thread splashscreen = new Thread() {
            public void run(){
                try{
                    int sp = 0;
                    while(sp < 1000){
                        sleep(100);
                        sp = sp +100;
                    };

                }

                catch (InterruptedException e) {
                    e.printStackTrace();
                }

                finally{
                    finish();
                    startActivity(new Intent(SplashScreenActivity.this, TutoActivity.class));
                }
            }
        };

        splashscreen.start();
    }

【问题讨论】:

  • 我不太明白。它每次都会启动,因为您启动屏幕是您的启动器活动。你不想从它开始吗?

标签: android screen splash-screen


【解决方案1】:

你应该像下面这样开始一个新的活动(如果用户按下后退按钮并返回到启动页面)

Intent intent = new Intent(this, A.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); 
    startActivity(intent);
    finish(); // call this to finish the current activity

【讨论】:

    【解决方案2】:

    原因

    由于 android 应用程序只能有一个启动器活动,因此我认为您的应用程序不会在每次最小化应用程序时显示启动画面,如果是,那么应用程序会在您恢复时重新启动。应用程序仍在重新启动而不是恢复的原因是您正在使用的设备没有足够的内存来容纳您的应用程序并为您提供流畅的多任务处理体验。

    解决方案

    您必须将您的应用程序安装在具有足够内存来运行您的应用程序的不同设备上。即使您将其最小化,这也应该可以防止您的应用重新启动。

    希望对你有帮助

    【讨论】:

      【解决方案3】:

      我想你已经得到了答案,但是为了良好的实践,永远不要让主线程休眠。 您可以使用它来延迟它而不是睡眠。

      int showSplashFor = 2500;
      
      new Handler().postDelayed(new Runnable() {
      
                  @Override
                  public void run() {
                      startActivity(new Intent(SplashScreen.this, MainActivity.class));
                      SplashScreen.this.finish();
                  }
              }, showSplashFor);
      

      【讨论】:

        【解决方案4】:

        好的,所以诀窍是先调用

        finish()
        

        然后

        startActivity()
        

        在您的 SplashScreenActivity 上,因此在您终止应用之前它不会出现。

        【讨论】:

          猜你喜欢
          • 2011-11-28
          • 1970-01-01
          • 1970-01-01
          • 2019-12-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-05-14
          相关资源
          最近更新 更多