Reputation: 557
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.
Upvotes: 2
Views: 389
Reputation: 146
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
Upvotes: 3