axehit

    axehit

    11 months ago
    Hello guys, I have a question regarding
    DisposableEffect
    . While using this effect I observed that if I move to another activity, then the
    onDispose
    lambda in the
    DisposableEffect
    of the composable in previous activity I navigated from didn’t get called. Is this an expected behaviour? That since I didn’t use compose navigation instead activity navigation
    onDispose
    isn’t called. And on the same topic can you suggest some other alternative of disposing data when composable moves out of view in case of activity navigation, my usecase is I want to send a tracking event everytime user moves out of screen.
    Albert Chang

    Albert Chang

    11 months ago
    onDispose
    is similar to activity's
    onDestroy
    . Unlike fragments and composables, when you move to a new activity, the previous activity isn't destoryed. It just stops.
    axehit

    axehit

    11 months ago
    Ok, got it, then is there some way in composable to know that it has move to background or onPause state analogous API?
    Albert Chang

    Albert Chang

    11 months ago
    You can observe the lifecycle of
    LocalLifecycleOwner.current
    in a
    DisposableEffect
    .
    axehit

    axehit

    11 months ago
    Thanks, I’ll try that out.
    i

    Ian Lake

    11 months ago
    The
    DisposableEffect
    docs actually show how to use that API for listening for lifecycle events: https://developer.android.com/jetpack/compose/side-effects#disposableeffect