android - White screen at app startup - Splash Screen


Keywords:android 


Question: 

This question already has an answer here:


3 Answers: 

Splash Activity usually starts with the background. Try this code, it might help you.

Code for your splash activity page.

public class SplashScreen extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash_screen);

Thread myThread = new Thread(){

    @Override
    public void run() {
        try {
            sleep(3000);
            Intent intent = new Intent(getApplicationContext(),MainActivity.class);
            startActivity(intent);
            finish();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
};
    myThread.start();
}}

In layout file, you need to include just Image which you want to see on the splash page.

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_splash_screen"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000">
   <ImageView
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:src=""
       />
</RelativeLayout>

And make sure you add the Splashscreen page before Main activity in Manifest.

 <activity android:name=".SplashScreen">
 

The problem is because system process draws initial blank screen when launching the app, from documentation:

A common way to implement a themed launch screen is to use the windowDisablePreview theme attribute to turn off the initial blank screen that the system process draws when launching the app. However, this approach can result in a longer startup time than apps that don’t suppress the preview window. Also, it forces the user to wait with no feedback while the activity launches, making them wonder if the app is functioning properly.

You can disable it with windowDisablePreview attribute, something like this:

  <application
      ...
      android:windowDisablePreview="true">
   ...
  </application>
 

you can use splash screen in this way.

  1. Add style for splash screen activity in styles.xml

    <style name="SplashTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:windowBackground">@drawable/yourImageName</item>
    </style>
    
  2. Add that style as theme to your SplashScreenActivity in manifest file

    <activity android:name=".SplashScreenActivity"
        android:theme="@style/SplashTheme"
        android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
    
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    
  3. Remove setContentView() in SplashScreenActivity's onCreate() Method and use it as a java file which extends AppCompactActivity