EliasMP EliasMP - 8 days ago 5
Android Question

Coding a Splash Screen

I´m trying to made an app which shows a first layout for 5 seconds and after going to the index layout (Home).
The first layout shows only a logo and the name of the app...

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SystemClock.sleep(5000);
Intent i = new Intent(this, Home.class);
startActivity(i);
}


If I comment the
SystemClock.sleep(5000); Intent i = new Intent(this, Home.class); startActivity(i);
, app works as I expected. Show the first layout, staying in this layout (obviously, I´m not launching anything else :P). But, If I descomment them, the activity wait for 5 seconds without showing anything (blank layout).

Any ideas?

Thanks guys.

Answer

In order to show a splash screen on your app you should not show a layout, try to put the main thread to sleep for 5 seconds and the wake the app again. The "right" way (there's no right way of showing a splash screen, as splash screens are almost forbidden on mobile apps) of showing a splash screen would be create a new Activity or Fragment that holds this screen and, using a Handler.postDelayed inside this activity, wait for 5 seconds and then show your main activity.

Useful information on Splash Screens:

https://www.bignerdranch.com/blog/splash-screens-the-right-way/

Comments