Stylianos Gakis
04/15/2024, 7:39 AMLifecycleStartEffect(Unit) {
val job = this.lifecycleScope.launch { ... }
onStopOrDispose {
job.cancel()
}
}
But might as well just do this instead?
val lifecycle = LocalLifecycleOwner.current.lifecycle
LaunchedEffect(lifecycle) {
lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) {
...
}
}
Something else I may be missing here instead? đStylianos Gakis
04/15/2024, 7:41 AMLifecycleStartEffect(Unit) {
val job = this.lifecycleScope.launch {
this.repeatOnLifecycle(Lifecycle.State.STARTED) {
...
}
}
onStopOrDispose {
}
}
But that looks a bit awkward both with the empty dispose block and with having to specify the state level the repeatOnLifecycle should be following đMagnus Gudmandsen
04/15/2024, 9:08 AMStarted
state), especially when working with bottom sheets and results that should trigger navigation to elsewhere.
It seems that this effect takes a while until the lifecycles are set again, so we get a race condition on handling the result, since the old coroutine that handled the navigation previously was cancelled.
Reading your post again... aren't we having opposite issues here?Stylianos Gakis
04/15/2024, 9:11 AMMagnus Gudmandsen
04/15/2024, 9:15 AMdropUnlessStarted {
LaunchedEffect(Unit) {
navigationEvents.collectLatest { navigate(it) }
}
}
Which feels a bit awkward...Stylianos Gakis
04/15/2024, 9:18 AMSomeScreen(
onNavigateToX = dropUnlessStarted { navController.navigateToX() }
)
Inside your screen you can observe that effect in whichever way you want. Then the lambda that you call can be this one, which will ensure it will only try to do the navigation when you are at least Started.
But with that said, if it is an event coming not from a user interaction, you probably do not want to drop it, since you will then leave it unconsumed as well. This however fits very well for normal âbutton clicked -> run thisâ scenarios. Where itâs ok to drop the event if for example someone clicked on a button twice really fastMagnus Gudmandsen
04/15/2024, 9:23 AMIan Lake
04/15/2024, 2:38 PMStylianos Gakis
04/15/2024, 3:23 PM