Hi all, looking to get a sanity check on lifecycleScopes…
My Fragment is doing a simple network request , and when it’s successful I’m navigating to another Fragment, when it fails I’m displaying an error
This works fine with process death, because the scope gets cancelled, but when the user backgrounds the app the coroutine still runs (because the view isn’t destroyed) and if successful it tries to navigate to the Fragment which fails because it’s after onSaveInstanceState()
Is this the ‘correct’ approach below, to basically wrap the Fragment transaction in
whenStarted
?
Copy code
viewLifecycleOwner.lifecycleScope.launch {
val response = fetchConfig()
when (response) {
is ConfigResponse -> {
whenStarted {
goToNextScreen()
}
}
Error -> showError()
}
}
m
Martyna Maron
02/25/2021, 4:54 PM
How does
whenStarted
help here? If you background the app and let the coroutine finish, on foregrounding the app, is