简体   繁体   中英

Splash screen only works on first install

I have implemented a splash screen that works great on only the first install/run of the app. Subsequent force stop/runs (after the app has already been run once) only show the splash screen quickly 'blink' on the screen. The splash screen activity's theme is all that is seen in this case.

SplashScreen.java

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

    mSplashDialog = new Dialog(this, R.style.Theme_Splash);
    mSplashDialog.setContentView(R.layout.normal_launch_screen);
    mSplashDialog.show();

    new DatabaseLoaderTask().execute();
}

DatabaseLoaderTask.onPostExecute

protected void onPostExecute(Void result) {
    super.onPostExecute(result);

    // will close this activity and launch main activity
    Intent i = new Intent(SplashScreen.this, myActivity.getClass());
    startActivity(i);

    // close this activity
    finish();
}

Manifest

<activity
    android:name="com.example.app.SplashScreen"
    android:label="@string/app_name"
    android:theme="@style/Theme_Splash" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

<activity
    android:name="com.example.app.MainActivity"
    android:label="@string/app_name" >
</activity>

the DatabaseLoaderTask executes much faster on subsequent runs which may be part of it. I almost think that on the first run, the splash waits until the MainActivity UI is ready but on subsequent runs it does not so the SplashScreen activity finishes prematurely and I wait staring at a blank screen for a few seconds (the Theme_Splash).

NOTE: I get the same result when instead of using a Dialog for the splash screen I just do a setContentView(R.layout.normal_launch_screen);

You're right. your splash will only display for how long it takes for the databaseloadertask to run. If you want to make sure it shows for a minimum amount of time, try something like this:

    mSplashDialog = new Dialog(this, R.style.Theme_Splash);
    mSplashDialog.setContentView(R.layout.normal_launch_screen);
    mSplashDialog.show();
   // Start timer and launch main activity
      MyLauncher launcher = new MyLauncher();
      launcher.start();
    }

     private class MyLauncher extends Thread {
      @Override
      /**
       * Sleep for some time and than load database.
       */
      public void run() {
         try {
            // Sleeping
            Thread.sleep(3000);
         } catch (Exception e) {
            Log.e("splash", e.getMessage());
         }

         new DatabaseLoaderTask().execute();
      }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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