Splash Screen on Android

Splash Screens are not exactly common (or really wanted) on Android. But occasionally there are customers that want that kind of thing. When I took a look around, I found a lot of solutions with different approaches. One of the more sophisticated ones used a thread to count the remaining seconds until it would fire an Intent to the actual start screen. That reminded me of AsyncTask aka "threads for the rest of us". ;)

So I came up with this solution:

private class SplashScreenDelay extends
        AsyncTask<Integer, Integer, Integer> {

    private static final String TAG = "SplashScreenTask";

    @Override
    protected Integer doInBackground(Integer... params) {
        try {
            int count = 0;
            while (count < params[0]*10) {
                if(count % 10 == 0) { Log.v(TAG, "Sleeping... " + count/10); }
                Thread.sleep(100);
                count++;
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return new Integer(0);
    }

    @Override
    protected void onPostExecute(Integer result) {
        startActivity(new Intent(getApplicationContext(), FirstStart.class));
    }

    protected void onCancelled() {
        startActivity(new Intent(getApplicationContext(), FirstStart.class));
    }
}

You can implement SplashScreenDelay as nested class within your activity. In "onCreate" you would start your new asynchronous timer with:

splashDelay = new SplashScreenDelay().execute(5);

I like the idea, that a user can skip the splash screen by just touching the screen. Therefore we need this in our activity:

@Override
public boolean onTouchEvent(MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        splashDelay.cancel(true);
    }
    return true;
}

Almost done! Now we just need to make sure, that the splash screen won't be in the history stack. The easiest way to do this is in your AndroidManifest.xml. Just add android:noHistory="true" to your activity.

Have fun!

Android Java