https://kotlinlang.org logo
#compose
Title
# compose
j

james

06/09/2021, 4:01 AM
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

Ian Lake

06/09/2021, 5:00 AM
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

james

06/09/2021, 5:07 AM
Thankyou, you were on the money, this all makes more sense now.
c

cb

06/09/2021, 6:49 AM
j

james

06/09/2021, 11:29 AM
Thanks great post!
3 Views