Splash Screen API For New App

Ujjawal Kumar
2 min readMar 27, 2024

--

So it all started when I got this suggestion while creating Splash Screen.

Usually this SplashScreenActivity is mainly responsible for

  • Fetching initial app/ user data (from server or db)
  • Deciding which screen to launch ( login or home)
  • App branding

Fetching initial app/ user data (from server or db)

Its not hard as we think, just need to add setKeepOnScreenCondition on splashScreen instance and you can pass logic for how long you want to keep your user on splash screen.

private var keepOnScreenState = true

override fun onCreate(savedInstanceState: Bundle?) {
setUpSplashScreen()
super.onCreate(savedInstanceState)
mockApiCall()
}

private fun setUpSplashScreen() {
val splashScreen = installSplashScreen()
splashScreen.setKeepOnScreenCondition {
keepOnScreenState
}
}

private fun mockApiCall() {
lifecycleScope.launch {
delay(3000) // mocked delay
keepOnScreenState = false
}
}

There is no magic in this, it is just that whenever android wants to draw its first frame, it checks for the value you’re returning and as soon as it gets false it draws the frame (here starts the activity).

Deciding which screen to launch

I had to create a single activity (MainActivity) to fix this.
my MainActivity installs the new splashScreen and when it finishes I add fragments based on data.

if (userData != null) {
addHomeFragment()
} else {
addLoginFragment()
}

App branding

For App branding I have asked my design team to design our animated app icon based on the guidelines provided in documentation.

https://developer.android.com/develop/ui/views/launch/splash-screen#elements

Thinks to remember

  • You need to set your activity theme to Spalsh theme.
  • Theme.App.Starting should have postSplashScreenTheme attribute.
<style name="Theme.App" parent="android:Theme.Material.Light.NoActionBar">
<item name="android:windowBackground">@color/white</item>
</style>
<style name="Theme.App.Starting" parent="Theme.SplashScreen.IconBackground">
<item name="android:windowBackground">@color/purple_500</item>
<item name="postSplashScreenTheme">@style/Theme.App</item>
</style>

To be continued….

--

--

No responses yet