Hi! How can we use ON_PAUSE and ON_RESUME lifecycl...
# compose-ios
a
Hi! How can we use ON_PAUSE and ON_RESUME lifecycle functions in Compose Multiplatform as in Jetpack Compose in Android?
i
androidx.lifecycle.*
is currently Android-only
a
Any hint at how we can do this?
i
1.5.10-beta01 adds view controller lifetime hooks - https://github.com/JetBrains/compose-multiplatform-core/pull/779
🎉 1
a
This would require support from a navigation library. Decompose provides this support, and I think Decompose-Router as well. Maybe other navigation libraries have it as well, not sure.
If you don't need navigation for some reason (e.g. your app has only one screen), then you could try using Essenty directly.
âž• 1
a
Thank you for the response. Can you please suggest how can I use it in Compose? Any sample?
a
There are samples and docs if you follow the links for the libraries. But there are no samples I'm aware of for just using the Essenty Lifecycle, without any library. But you can check the readme, it's pretty easy.
a
https://github.com/arkivanov/Essenty#lifecyle I want to use Essenty, but there’s no sample here.
@Ivan Matkov How can we use these hooks? Can you please share any sample?
a
With Essenty you can use
LifecycleRegistry
. Then just override
applicationDidBecomeActive
,
applicationWillResignActive
and
applicationWillTerminate
and drive the lifecycle. Take a look here. Then you can create a composition local for
Lifecycle
so that it's available in every Composable.
Or if you are using
ComposeUIViewController {}
function, then there is now the
delegate
property that can be used instead.
a
Thank you for the response. How can we use this delegate property?
a
I guess something like this:
Copy code
ComposeUIViewController {
    val lifecycle = remember { LifecycleRegistry() }

    delegate = 
        object : ComposeUIViewControllerDelegate {
            override fun viewWillAppear() {
                lifecycle.resume()
            }

            override fun viewDidDisappear() {
                lifecycle.stop()
            }
        }

    // The rest of the code
}
Though, I don't see any callback equivalent to onDestroy. In Swift we have
deinit
. So perhaps you could create and control the lifecycle in Swift and then just pass it to Compose and make it available with a composition local. Or, if you have just one Composable for the entire app, then you can implement
UIApplicationDelegate
instead, which gives
applicationWillTerminate
callback.
a
Thanks! Let me check
426 Views