Colton Idle
10/06/2023, 7:02 PMPablichjenkov
10/06/2023, 7:04 PMColton Idle
10/06/2023, 7:05 PMIan Lake
10/06/2023, 7:27 PMlifecycle-runtime-compose
that has the new LifecycleEventEffect
for a single event and LifecycleStartEffect
/ LifecycleResumeEffect
for a disposable effect style pair of events: https://developer.android.com/jetpack/androidx/releases/lifecycle#2.7.0-alpha01Colton Idle
10/07/2023, 1:28 AMIan Lake
10/07/2023, 1:32 AMDisposableEffect
needs to end in a onDispose
block? LifecycleStartEffect
needs to end in a onStopOrDispose
block - that's the "pair of events" I mentioned: https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:lifecycl[…]idx/lifecycle/compose/samples/LifecycleComposeSamples.kt;l=92Colton Idle
10/07/2023, 3:41 PMLifecycleStartEffect {
dataAnalytics.startTimeTracking()
onStopOrDispose {
timeTracker.stopTimeTracking()
}
}
equivallent to this?
LifecycleEventEffect(Lifecycle.Event.ON_START){
dataAnalytics.startTimeTracking()
}
LifecycleEventEffect(Lifecycle.Event.ON_STOP){
dataAnalytics.stopTimeTracking()
}
Ian Lake
10/07/2023, 5:56 PMOrDispose
part of onStopOrDispose
should be a clue on where they differ: if the LifecycleStartEffect
is removed from composition (say, it is inside an if statement that changes from true to false) before the Lifecycle is stopped, the onStopOrDispose
block will still fire, ensuring the cleanup of whatever you did before that block is guaranteed to run if you ever got STARTED.
Having two separate blocks won't have that property - if you put your code in separate LifecycleEventEffect
blocks, there's no coupling between them and thus no guaranteed cleanupMarcin Wisniowski
07/11/2024, 3:58 PMonStopOrDispose
gets called twice (if you navigate out of a screen, it gets fired both for the STOP lifecycle event, and again for being disposed) which is a problem if the cleanup is not idempotent. Is this intended?
In the above example stopTimeTracking()
would get called twice which depending on the implementation may be a problem, where having the two separate `LifecycleEventEffect`s doesn't have that problem.Ian Lake
07/11/2024, 4:11 PMMarcin Wisniowski
07/11/2024, 4:26 PM