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 cleanup