https://kotlinlang.org logo
#compose-android
Title
# compose-android
c

Colton Idle

10/06/2023, 7:02 PM
My google-fu is failing me. What was the "new" way to observe android lifecycle events like onCreate, onStart, etc in compose. I used to use disposableEffect, but there was some artifact that was updated with this I recall...
p

Pablichjenkov

10/06/2023, 7:04 PM
I don't think it is out yet, unless it was really recent
c

Colton Idle

10/06/2023, 7:05 PM
too many release notes ive read. i forget whats what. 😂
😁 1
i

Ian Lake

10/06/2023, 7:27 PM
It was the 2.7.0-alpha01 release of
lifecycle-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-alpha01
c

Colton Idle

10/07/2023, 1:28 AM
Do you by chance know if there are any sample usages of this. I can't seem to get LifecycleStartEffect to compile. feel like im mis-understanding something here
i

Ian Lake

10/07/2023, 1:32 AM
You know how
DisposableEffect
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=92
❤️ 4
🙏 3
🙏🏾 1
c

Colton Idle

10/07/2023, 3:41 PM
I'm going to test this out myself... but figured I'd ask since we have the thread going anyway. Is this
Copy code
LifecycleStartEffect {
            dataAnalytics.startTimeTracking()

            onStopOrDispose {
                timeTracker.stopTimeTracking()
            }
        }
equivallent to this?
Copy code
LifecycleEventEffect(Lifecycle.Event.ON_START){
dataAnalytics.startTimeTracking()
  }

LifecycleEventEffect(Lifecycle.Event.ON_STOP){
dataAnalytics.stopTimeTracking()
  }
i

Ian Lake

10/07/2023, 5:56 PM
The
OrDispose
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
👍 2
2 Views