If I subscribe to a flow from a `ViewModel` using ...
# android
d
If I subscribe to a flow from a
ViewModel
using
launchIn
on a `Fragment`'s
lifecycleScope
can I assume that the UI won't get updated if the activity is detached or the state is paused? And if not, then do I have to register all these things on
onResume
and cancel them on
onPause
or check before updating each time?
h
It depends on what kind of coroutine you launch. There are
launchWhenCreated, launchWhenStarted, launchWhenResumed
builder functions that you can use to launch coroutines. They all lifecycle aware.
d
This flow needs to send updates to the ui, so if I use one of those on oncreated, it will delay the starting of the collection, but won't stop it at pause and won't resume it at resume, unless i'm understandong wrong?
h
if you want get events when your view resumed you can use this
launchWhenResumed
in case of start
launchWhenStarted
and it will pause listening when view is not resumed or is not started accordingly
d
But how do you do that for `Flow`s?
Copy code
someFlow().launchIn(...?)
// Or do I need to do this:
lifecycleScope.launchWhenResumed {
    someFlow.collect { }
}
h
Yes the second one
and StateFlow instead of Flow i guess
d
Oh well, the first one looks much better..., I guess we'll need to change that, thanks!