Marco Pierucci
02/21/2025, 12:46 PMpresent
composable function. There we lunch a coroutine to execute some logic.
The coroutine uses an application scope as we want the execution to continue even if the user leaves the screen, but when we get the result we don't want to pop if we are not "on that screen"Zach Klippenstein (he/him) [MOD]
02/21/2025, 3:39 PMvar active by remember { mutableStateOf(true) }
DisposableEffect {
appScope.launch {
doThing()
if (active) pop()
}
onDispose { active = false }
}
Or do it just with coroutines:
LaunchedEffect {
val job = appScope.launch {
doThing()
}
job.join()
pop()
}
Marco Pierucci
02/21/2025, 3:40 PMvar isPresenting by remember { mutableStateOf(true) }
// Track lifecycle of present function
LaunchedEffect(Unit) {
isPresenting = true
}
// When composable gets removed, deactivate it
DisposableEffect(Unit) {
onDispose {
isPresenting = false
}
}
and
onSuccess = {
if (isPresenting) {
navigator.pop()
}
},
Marco Pierucci
02/21/2025, 3:41 PMMarco Pierucci
02/21/2025, 3:42 PMZach Klippenstein (he/him) [MOD]
02/21/2025, 3:43 PMZach Klippenstein (he/him) [MOD]
02/21/2025, 3:44 PMMarco Pierucci
02/21/2025, 3:45 PMMarco Pierucci
02/21/2025, 3:46 PMZach Klippenstein (he/him) [MOD]
02/21/2025, 3:51 PMLaunchedEffect {
appService.doThing()
pop()
}
and not worry about all the lifecycle management stuff that’s really not its concern. Keeping the violation of SC in a separate function also makes it a lot easier to test, which is extra important in these cases.Marco Pierucci
02/21/2025, 3:54 PM