Why we must pass lifecycleOwner as key to Effects ...
# compose
e
Why we must pass lifecycleOwner as key to Effects although known that it never changes until Container destroy?
🖼️ 10
v
No, we must not.
👎 2
But if can, it it fits for a case
Nice screenshot, need to say
😂 4
i
It can change though - if you use Navigation Compose and use a singleTop operation, you'll get a new NavBackStackEntry (and hence a new LifecycleOwner) for the same composition
1
t
In the next compose version will be effects for lifecycle things: https://developer.android.com/reference/kotlin/androidx/lifecycle/compose/package-summary If you want to use it you need to add dependency:
Copy code
implementation("androidx.lifecycle:lifecycle-runtime-compose:2.7.0-alpha01")
i
If you reference something within an Effect block, particularly a DisposableEffect, it most certainly should be a key
e
IMG_20230802_165228.jpg
s
I think the most important thing to keep here is this https://kotlinlang.slack.com/archives/CJLTWPH7S/p1690982763020769?thread_ts=1690981444.759929&cid=CJLTWPH7S Whenever you reference anything inside a side effect, assume it will change at some point and always key your effect properly. If it really never changes, then adding it as a key would again not make it restart, so it’s always a right idea to key correctly, regardless of if you are sure that something won’t change (often it’s a wrong assumption to make anyway).
i
Those new Lifecycle APIs don't require you to wait for a new Compose version, you can upgrade to Lifecycle 2.7.0-alpha01 no matter what Compose version you are on: https://developer.android.com/jetpack/androidx/releases/lifecycle#2.7.0-alpha01
☝️ 2
e
That's right, thank you. For a moment, navigation was completely out of my mind
p
It is a good practice using keys either in Disposable effect or remember. Somebody correct me if wrong, but I believe your Composable functions may execute over 2 containers as different recomposition happens. At the end the Composables are global and don't control what container was passed as state
v
Ian do you have any info or any eta of those new lifecycle APIs for common part of the compose (multiplatform)?