I create an app with Code A made by Android Jetpack Compose, It shows a blank UI long time before main UI is displayed, you can see Image A
I learned some sample project, such as the official sample project Crane.
So I modify Code A as Code B based Crane, I hope Code B to display a Loading UI quickly before main UI is displayed, but I still find it shows a blank UI long time before main UI is displayed with Code B.
BTW, I find the sample project Crane also shows a blank UI long time before main UI is displayed, you can see Image B.
A: How can I show a Loding UI quickly before main UI is displayed?
B: I can't understand Code B fully, why does the Loding UI dismiss automatically? does it due to the API function MutableTransitionState?
C: In Code B, even if I set val SplashWaitTime: Long = 10000000, I find the Loding UI dismissed about 2 sencond later just like I set val SplashWaitTime: Long = 2000, why?
Code A
@AndroidEntryPoint
class ActivityMain : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
SoundMeterTheme {
Surface(color = MaterialTheme.colors.background) {
NavGraph()
}
}
}
}
}
Image A
Code B
@AndroidEntryPoint
class ActivityMain : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
SoundMeterTheme {
Surface(color = MaterialTheme.colors.background) {
val transitionState = remember { MutableTransitionState(SplashState.Shown) }
LandingScreen(
onTimeout = { transitionState.targetState = SplashState.Completed }
)
NavGraph()
}
}
}
}
}
private const val SplashWaitTime: Long = 2000
enum class SplashState { Shown, Completed }
@Composable
fun LandingScreen(modifier: Modifier = Modifier, onTimeout: () -> Unit) {
Box(modifier = modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
val currentOnTimeout by rememberUpdatedState(onTimeout)
LaunchedEffect(Unit) {
delay(SplashWaitTime)
currentOnTimeout()
}
Text("Loading..")
}
}
Image B

