2

I implemented the lifecycle detector for iOS in the Compose Multiplatform Project using the below code.

val lifecycleDelegate: ComposeUIViewControllerDelegate = object : ComposeUIViewControllerDelegate {
   override fun viewDidAppear(animated: Boolean)
   override fun viewDidLoad()
   override fun viewWillDisappear(animated: Boolean)
   override fun viewWillAppear(animated: Boolean)
   override fun viewDidDisappear(animated: Boolean) 
}

fun MainViewController() = ComposeUIViewController(
    configure = {
        delegate = lifecycleDelegate
    }
) {
    KoinApplication(application = {
        init()
    }) {
        App()
    }
}

The methods viewDidLoad, viewWillAppear, and viewDidAppear are invoked once during the lifecycle of ViewController. However, the methods viewDidDisappear, viewWillDisappear are not being called as expected. Any insights or solutions to address this behavior would be greatly appreciated.

3
  • i am also facing the same issue Commented May 25, 2024 at 5:52
  • yes, i had done it Commented May 28, 2024 at 9:36
  • you can see the events from this repo(github.com/TheReprator/ExpenseManagment) Commented Jun 3, 2024 at 4:15

1 Answer 1

4

Starting in the 1.6.10-beta01 release of Compose MP, you can use the AndroidX Lifecycle library in your multiplatform sources. It already maps all ViewController states to the AndroidX Lifecycle.

The usage is quite simple, you just need to get the owner from the Local Composition and then you can use it the same way you would do on Android.

@Composable
fun MyComposable() {
    val lifecycleOwner = LocalLifecycleOwner.current
    // Do something with the lifecycle
}

For more details, check the official documentation available in the Kotlin website: https://www.jetbrains.com/help/kotlin-multiplatform-dev/compose-lifecycle.html#ios

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.