Thread
#compose
    j

    james

    1 year ago
    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

    1 year ago
    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
    j

    james

    1 year ago
    Thankyou, you were on the money, this all makes more sense now.
    cb

    cb

    1 year ago
    j

    james

    1 year ago
    Thanks great post!