Optimising Android Launch: Mastering App Startup & The Modern Splash Screen
The first few seconds of an app’s life determine a user's first impression. A slow start or a flickering white screen can lead to immediate uninstalls. In this guide, we’ll look at how to streamline your initialisation using the App Startup Library and ensure a seamless visual transition with the Android 12 Splash Screen API (with full backward compatibility for API 29).
Part 1: The App Startup Library
Traditionally, apps initialised multiple components using separate content providers. This adds overhead and slows down launch time. The Jetpack App Startup library allows all components to share a single content provider, significantly improving performance.
Why use it?
Performance: Shared content provider reduces overhead.
Order: Explicitly set the initialisation sequence.
Simplicity: Both library creators and app developers use a unified interface.
Part 2: The Modern Splash Screen API
Starting in Android 12, the OS introduced a native splash screen. For older systems (like API 29/Android 10), we use the core-splashscreen library to bridge the gap.
If your app shows a "white flicker" on older devices, it’s usually because the postSplashScreenTheme hasn't been set correctly or initialisation is happening on the main thread.
Step 1: Configure Your Themes
Update your res/values/themes.xml to include two distinct themes: the "Starting" theme and the "Main" theme.
<resources>
<style name="Theme.App.Starting" parent="Theme.SplashScreen">
<item name="windowSplashScreenBackground">@color/background_color</item>
<item name="windowSplashScreenAnimatedIcon">@mipmap/ic_launcher</item>
<item name="postSplashScreenTheme">@style/Theme.Book</item>
</style>
<style name="Theme.Book" parent="Theme.Material3.DayNight.NoActionBar">
<item name="android:windowBackground">@color/background_color</item>
<item name="android:statusBarColor">@android:color/transparent</item>
</style>
</resources>
Step 2: Manifest Setup
Point your Application and Launcher Activity to the Starting theme.
<application
android:name=".BookApplication"
android:theme="@style/Theme.App.Starting">
<activity
android:name=".MainActivity"
android:exported="true"
android:theme="@style/Theme.App.Starting">
</activity>
</application>
Part 3: Implementing in MainActivity
To prevent the screen from disappearing before your data (like Billing or User Preferences) is ready, use setKeepOnScreenCondition.
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
// 1. Install splash screen before super.onCreate
val splashScreen = installSplashScreen()
super.onCreate(savedInstanceState)
var isReady = false
// 2. Load critical data (DataStore, Billing Manager, etc.)
lifecycleScope.launch {
// Wait for your initialization logic here
isReady = true
}
// 3. Keep the splash screen visible until data is loaded
splashScreen.setKeepOnScreenCondition { !isReady }
setContent {
if (isReady) {
BookTheme {
MainAppScreen()
}
}
}
}
}
Common Troubleshooting: The API 29 White Screen
On Android 10 (API 29), the system doesn't have a native splash screen. The core-splashscreen library draws a "fake" view. If you experience flickering:
Check for Startup Conflicts: Ensure libraries like Google Mobile Ads are initialized on a background thread (
Dispatchers.IO) inside your App Startup initializers.Match Backgrounds: Ensure your
android:windowBackgroundin your main theme exactly matches the color you use in your Compose/XML layout.Synchronous Tasks: Avoid accessing large databases on the Main Thread during
onCreate.
Official Resources
Happy Coding!

Comments
Post a Comment