I currently have a flat compose navigation hierarc...
# compose
j
I currently have a flat compose navigation hierarchy, and when navigating from the 'home' screen to another screen, any hot flows tied to the viewModelScope of the 'home' screen will continue to operate as expected. I want the viewModel to stay alive, but I want the flows paused while not inside that composable, so I load/dispose them inside the 'home' composable via a DisposableEffect with job cancellation. Is there a more sensible way to handle this?
i
There's a lot to unpack here. A hot flow is, by definition, always going, so there's no such thing as 'pausing' it. If you're using something like
shareIn()
or
stateIn
, then it sounds like you should be using
SharingStated.WhileSubscribed()
, which would automatically stop the upstream collecting when your
collectAsState()
/
collect
within a
LaunchedEffect
cancels (i.e., you move to another screen). There's some more discussion around how that works at https://developer.android.com/kotlin/flow/stateflow-and-sharedflow
👍 1
j
Thankyou, you were on the money, this all makes more sense now.
c
j
Thanks great post!