Hey there. I have some misunderstandings regarding...
# multiplatform
m
Hey there. I have some misunderstandings regarding the Lifecycles of a compose-multiplatform. When I use the code below, onEvent is triggered more than enough. It happens when I minimize and reopen the app or reopen the screen where this composable is used. Log: Event: ON_CREATE Event: ON_START Event: ON_RESUME Event: ON_RESUME //Minimize the app and reopen the app Event: ON_PAUSE Event: ON_PAUSE Event: ON_STOP Event: ON_STOP Event: ON_START Event: ON_START // an extra call Event: ON_RESUME Event: ON_RESUME Event: ON_RESUME // an extra call ..... The log is getting bigger every time I repeat the process. If I repeat this process n times, it's making n more extra calls. How can I make it work correctly? Or is this an expected behaviour?
Copy code
@Composable
fun ComposableLifecycle(
    lifecycleOwner: LifecycleOwner = LocalLifecycleOwner.current,
    onEvent: (Lifecycle.Event) -> Unit
) {
    DisposableEffect(lifecycleOwner) {
        val observer = LifecycleEventObserver { _, event ->
            Log.d { "Event: ${event.name}" }     // print the log
            onEvent(event)
        }
        lifecycleOwner.lifecycle.addObserver(observer)

        onDispose {
            lifecycleOwner.lifecycle.removeObserver(observer)
        }
    }
}
Here is the same behavior on Android & iOS. Dependency: "org.jetbrains.androidx.lifecyclelifecycle viewmodel compose2.8.0"
p
Sounds like a bug, you shouldn't have duplicates
But it could be the case you are registering more than one observer. Can you print a unique ID in the observer to confirm is not the case