We all know that Splash screens in Android are very useful if there is some starting background operation that user doesn’t need to see, but takes time to load (for example if the user is logged in or not, some reading of content, checking location, permissions,,,). In this scenario, the Splash screen comes in handy. Almost every time there is a logo, or simple text of the application. After all background operations are done, the application takes you inside.

But what happens if there is no background operations at all? Application that is not connected to the internet, or simple app that does everything in it self, but we want to add Splash at app start?

Here is the thing. First we declare an SplashActivity that is launcher, and we declare it in the AndroidManifest:

Then we build our SplashScreen activity like this:

We declare Handler and Runnable at first and we will use them in our delay() method. A Handler allows you to send and process Message and Runnable objects associated with a thread’s MessageQueue. There are two main uses for a Handler: (1) to schedule messages and runnables to be executed at some point in the future; and (2) to enqueue an action to be performed on a different thread than your own. In our example we are going to use it with Runnable. The Runnable interface should be implemented by any class whose instances are intended to be executed by a thread. The class must define a method of no arguments called run. In this run method, we start the next activity, which in our case is Home activity, and we call finish() on Splash screen, so it can be executed only once on every app start, and it cannot be available anymore in case back is pressed on HomeActivity.

After this is made, we call postDelayed() method with our two arguments, previously declared runnable and long value in milliseconds. This time tells us that in 2000 milliseconds the operation declared in the run() method will be executed. Simple, the splash screen will remain 2 seconds on screen and after that the Home Screen will be displayed.

We need to override onPause() method to remove handler callbacks with our runnable, so in that 2 seconds time, if back or home are pressed the app wont go in Home Screen.

Here is the XML layout file for the Splash Activity Screen:

The code for Home Activity Screen:

And the XML layout for Home Activity Screen:

This is the style used:




Spread the love

1 Comment

  1. Hey There. I found your blog using Bing. This is a very well written article. I’ll be sure to bookmark it and come back to read more of your useful information. Thanks for the post.

Leave a Reply

Your email address will not be published.