Search code examples
androidandroid-activityactivity-lifecycle

Launcher activity setContentView delay


I created a simple app with Splash screen which is shown for 1 second. Splash screen is almost red. But I noticed that when I launch my app at first for ~0.3 sec appears white screen and after that appears my Splash screen. Is it possible to remove this white screen or make it predefined red? I testes it on nexus 4 with android 5.0, and my Splash activity have quite simple implementation, and nothing can delay onCreate():

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_launcher);
    waitTask = new WaitTask();
    waitTask.execute();
}

private class WaitTask extends AsyncTask<Void, Integer, Void> {

    @Override
    protected Void doInBackground(Void... params) {
        try {
            TimeUnit.MILLISECONDS.sleep(SPLASH_DURATION);
        } catch (InterruptedException e) {
            // Do nothing
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        Intent intent = new Intent(LauncherActivity.this, MainActivity.class);
        startActivity(intent);
        finish();
    }

}

I have noticed that almost all apps show it first screen with some delay and this white screen, but in some app this screen is back. Is there any way to control it?


Solution

  • You can define the style of the activity and set it in the Manifest. This way, when the activity runs, it will use its pre defined style (in which you will have the background or windowBackground set to red) and within that 0.3 seconds, the windows will still be red.

    Define the colour:

    <resources>
        <color name="background">#FF0000</color>
    </resources>
    

    Then define the style:

    <resources>
        <style name="MyLaunchActivityTheme" parent="@android:style/Theme.Light"> 
            <item name="android:windowBackground">@color/background</item>
         </style>
    </resources>
    

    Then set the style for the activity in the manifest:

    <activity
        android:name=".MyActivity"
        android:theme="@style/MyLaunchActivityTheme"/>