White screen on app startup - Splash Screen

Asked

Viewed 1,729 times

-1

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

  • Edit the question to post the code you used to create this Splash Activity. Java and XML.

2 answers

3


First, create a drawable with the following content, its name 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>

Done this, you will in your Styles.xml put this snippet of code:

<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 statement of your Activity, remembering that it has to be the Auncher and main 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>

Ai on Activity’s onCreate, you can do all the necessary processing. It doesn’t 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();
    }

1

Just by 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 appear quickly.

Follow the example code.

 new Handler().postDelayed(() -> {
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
        finish();
}, 2000);
  • Yes yes, so I commented that you have to put any processing in before initializing Activity. In addition, delays of this kind are against google’s design rules. As calls, validations, among other actions already consume time, a splash has to stay as short as possible for the user, so it is recommended to use only the time necessary to process and then already display the real content of the app.

  • Correct. Is that I had understood that the processing to which you referred, was related only to the loading of the main screen.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.