Splash screens are not so popular in Android development. But wait: now Google started to put splash screens in its apps??? Take a look at Google Drive app for example. Does it mean that now we have a new standard?
This is not the right post to talk about if splash screen in Android is good or bad. Let’s think only about code: implementing a splash screen is easy.
I’ve seen many splash screen implementations, but some of them have one common bug. If I close the app (aka: press back button) when the splah screen is visibile, after a while the main screen of the app appears. WTF??? I’ve closed you, why are you coming back again?
The trick is very simple. Let’s take a look at how a very simple SplashActivity could be:
|
|
This is very straightforward. The key point here is the removeCallbacks
method: this is where I’m assuring that MainActivity is not started if the user have pressed back button (explicitly closing the application) or if app has gone in background.
In your AndroidManifest.xml don’t forget to declare your activity with the following attributes:
<activity
android:name=".SplashActivity"
android:configChanges="orientation|keyboardHidden"
android:label="@string/application_name"
android:noHistory="true" >
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>