Implement Default Splash Screen Prior to Android 12
Step-by-step Guide to Implement Default Splash Screen in Android 12 (API level 31) for Older Android Version
If your app doesn't implement any splash screen, it is implemented by default in Android 12 (API level 31). In other words, there is a splash screen for Android 12 or later but not for Android 11 and older Android version.
To stay consistent with the older Android version to have a similar default splash screen, you can follow this step-by-step guide to implement the splash screen.
1. Add Splash Screen Library
Add androidx.core:core-splashscreen
library in build.gradle.
dependencies {
implementation 'androidx.core:core-splashscreen:1.0.0'
}
2. Add Splash Screen Style
Add this splash.xml in your res/values folder. It can be any name you wish. You can also use the existing themes.xml if you have it.
<resources>
<style name="Theme.App.Starting" parent="Theme.SplashScreen">
<item name="windowSplashScreenBackground">#ff616161</item>
<item name="windowSplashScreenAnimatedIcon">@drawable/ic_android_kotlin_weekly</item>
<item name="postSplashScreenTheme">@android:style/Theme.Material.Light.NoActionBar</item>
</style>
</resources>
Set the theme style parent to
Theme.SplashScreen
Set
windowSplashScreenBackground
to your app primary colorSet
windowSplashScreenAnimatedIcon
to your app icon assetSet
postSplashScreenTheme
to your original theme style for your app
Please note that I hard code the color instead of using
@color/...
because I have gotten rid of this colors.xml completely in this clean compose app template.
In fact, I think we shouldn't need to have this splash.xml
at all for Jetpack Compose app.
3. Update Application Theme Style
In AndroidManifest.xml, update application android:theme
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<application
android:theme="@android:style/Theme.Material.Light.NoActionBar">
</application>
</manifest>
with @style/Theme.App.Starting
that you created in splash.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<application
android:theme="@style/Theme.App.Starting">
</application>
</manifest>
4. Call installSplashScreen() in Activity
In your activity, call the installSplashScreen()
, before you call the Acitivty.onCreate()
base class.
To decide how long the splash screen stay, you can call SplashScreen.setKeepOnScreenCondition()
which allows you to implement KeepOnScreenCondition
interface using SAM conversion.
This is an example of what I did for my rss feed reader app
override fun onCreate(savedInstanceState: Bundle?) {
setupSplashScreen()
super.onCreate(savedInstanceState)
/*...*/
}
private fun setupSplashScreen() {
var keepSplashScreenOn = true
lifecycleScope.launch {
lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) {
viewModel.uiState.collect { uiState ->
keepSplashScreenOn =
uiState is ArticlesUiState.Loading ||
uiState is ArticlesUiState.Invalid
}
}
}
installSplashScreen().setKeepOnScreenCondition {
keepSplashScreenOn
}
}
Summary
Your app now should have the splash screen in all Android versions. This is a default splash screen implementation which is pretty simple. For more details, you can refer to this official guide.
If the splash screen doesn't work, you may want to make sure your app is completely killed because the splash screen is NOT shown during hot start. This seems to happen in API 33 where pressing the back button doesn't kill the app. It is still in the background.
Reference
Code Changes Example: Diff(Android News) and Diff(Template App)