Hi folks! Is this the right way to show SplashScre...
# compose
a
Hi folks! Is this the right way to show SplashScreen or there is a better way?
Copy code
@Composable
fun SplashScreen(navController: NavController) {
    SplashScreenContent()
    val coroutineScope = rememberCoroutineScope()
    SideEffect {
        coroutineScope.launch {
            delay(2000)
            navController.popBackStack()
            navController.navigate(Screen.Home.route)
        }
    }
}
p
I'm pretty sure you get a "System" SplashScreen from the OS if the OS Version is >= Android 12, so this would lead to two consecutive SplashScreens. Also in general I think it's not a good practice to force a delay and show a screen like this without any reason as it will make the app look slow. Apart from that I would replace
SideEffect
by
LaunchedEffect(Unit)
2
l
you should never manually set a manual delay just to show a screen that blocks user interactions. There is many better option, for example using style/theme:
Copy code
<style name="SplashTheme" parent="@android:style/Theme.Material.Light.NoActionBar">
    <item name="android:windowBackground">@drawable/splash</item>
</style>
In your Manifest:
Copy code
<application
    ...
    android:theme="@style/SplashTheme">
👍 5
👍🏼 1
a
thanks guys, using a launcher theme is much better option!
a
+1 to using LaunchedEffect instead of SideEffect+CoroutineScope.launch if you find yourself wanting to do things of a similar shape. SideEffect will run every time it's recomposed and CoroutineScope.launch is not an idempotent operation
👍 4
👍🏼 1
c
Android 11 and lower, use the theme approach Android 12 and up, there's new splash apis
s
One more thing to take care of between different Android versions 😞
a
I have implemented SplashScreen with launcher theme (without separate SplashActivity, just using theme for MainActivity). The good thing is that there is only single splash screen on android 12, it shows default app icon in the center but looks like that can be changed with
android:windowSplashScreenAnimatableIcon
as mentioned in the docs - https://developer.android.com/about/versions/12/features/splash-screen