White screen at app initialization - Splash Screen

-1

I created a splash screen for my application using an empty activity that is visible for 3 seconds with a background image. Typically, the application starts with a white screen before the background image is visible, however, some applications are already started with the "real" home screen image. How do I implement this?

    
asked by anonymous 28.02.2018 / 04:16

2 answers

2

First, create a drawable with the following content, the name of it being background_splash:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/gray"/> <!--aqui pode ser qualquer coisa, até uma imagem-->
    <item>
        <bitmap 
            android:gravity="center"
            android:src="@mipmap/ic_launcher"/>
    </item>
</layer-list>

Once you have done this, you will in your styles.xml place this code snippet:

<style name="Splash" parent="Theme.AppCompat.NoActionBar">
    <item name="android:windowBackground">@drawable/background_splash</item>
</style>

Then, in your manifest, you will put the following in the declaration of your activity, remembering that it has to be the launcher and main of the project:

<activity
    android:name=".SplashActivity"
    android:theme="@style/Splash">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />    
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

Ah in the activity onCreate, you can do all the processing needed. It does not even need to have the setContentView () statement:

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

        //Aqui você faz todo e qualquer processamento necessário, depois inicializa sua MainActivity e finaliza a splash, pra tirar ela do back stack.
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
        finish();
    }
    
28.02.2018 / 12:41
1

Just complementing the friend's response above, you need to add a delay when creating the Splash screen, otherwise the code will load MainActivity instantly and the Splash screen will pop up quickly.

Follow the example code.

 new Handler().postDelayed(() -> {
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
        finish();
}, 2000);
    
28.02.2018 / 15:22