arkivanov / Essenty

The most essential libraries for Kotlin Multiplatform development
Apache License 2.0
468 stars 15 forks source link

Unable to get LifeCycle event for the KMP #164

Closed himanshumistri closed 3 months ago

himanshumistri commented 3 months ago

In Common code of Compose Multiplatform,

I have the below code.

 LaunchedEffect("LifeCycle"){
        val callbacks: Lifecycle.Callbacks =
            object : Lifecycle.Callbacks {
                override fun onCreate() {

                }

                override fun onStart() {

                }

                override fun onResume() {

                }

                override fun onPause() {
                    Logger.i (messageString = "Home Screen LaunchedEffect onPause", tag = "Home")
                }

                override fun onStop() {
                    Logger.i (messageString = "Home Screen LaunchedEffect onStop", tag = "Home")
                }

                override fun onDestroy() {

                }
            }

        viewModel.lifeCycleCallBack = callbacks
        mAppLifeCycleOwner.lifecycle.subscribe(callbacks)
        Logger.i (messageString = "Home Screen LaunchedEffect lifeCycleCallBack register", tag = "Home")
    } 

I does not trigger, I am testing it with Android for now, May be I am missing something...

I am also try below as well

private var mlifecycle :LifecycleRegistry = LifecycleRegistry(initialState = Lifecycle.State.INITIALIZED)

In my Android Native activity.

  override fun onStop() {
          super.onStop()
          mlifecycle.onStop()
          Logger.i (messageString = "onStop", tag = "MainActivity")
      }
arkivanov commented 3 months ago

It would be nice if you could provide a complete reproducer, as the provided code is incomplete.

himanshumistri commented 3 months ago

Here you go with my native main activity code of android : https://github.com/himanshumistri/KMPStarterKit/blob/main/androidApp/src/main/java/com/bumble/starter/MainActivity.kt

Here is my Compose Multiplatform Code where I need lifecycle event callback.

https://github.com/himanshumistri/KMPStarterKit/blob/main/shared/src/commonMain/kotlin/com/bumble/appyx/shared/child/ChildNode1.kt

I am not getting any event in ChildNode1

arkivanov commented 3 months ago

I think your implementation has multiple issues. Here are some of them.

  1. AbcAppLifeCycle implements Lifecycle, but the registered callbacks are never called.
  2. I don't think that AbcAppLifeCycle gets somehow connected with the LifecycleRegistry defined in MainActivity.
  3. In the MainActivity you are calling essentyLifecycle() but you are not using the returned Lifecycle. You could just use that instead of LifecycleRegistry.

Since you are using Appyx, I suggest reaching out to the developers and asking for help with lifecycle handling. Maybe there is an out-of-the box way. Or just use Decompose for navigation.

arkivanov commented 3 months ago

Closing this for now. Feel free to reopen if there are any questions or issues related to the Essenty library.