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

axehit

10/15/2021, 10:04 AM
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.
a

Albert Chang

10/15/2021, 10:58 AM
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.
👍 2
a

axehit

10/15/2021, 11:23 AM
Ok, got it, then is there some way in composable to know that it has move to background or onPause state analogous API?
a

Albert Chang

10/15/2021, 11:25 AM
You can observe the lifecycle of
LocalLifecycleOwner.current
in a
DisposableEffect
.
a

axehit

10/15/2021, 11:35 AM
Thanks, I’ll try that out.
i

Ian Lake

10/15/2021, 6:54 PM
The
DisposableEffect
docs actually show how to use that API for listening for lifecycle events: https://developer.android.com/jetpack/compose/side-effects#disposableeffect
👍 2
22 Views