Hi all, looking to get a sanity check on lifecycle...
# android
l
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
How does
whenStarted
help here? If you background the app and let the coroutine finish, on foregrounding the app, is
goToNextScreen()
triggered? According to the docs should the
fetchConfig
be wrapped in
whenStarted
too? 🤔
l
Yes that’s right, if I don’t use
whenStarted
then
goToNextScreen()
gets triggered when the app is in the background leading to an exception
a