arkivanov / Essenty

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

Basic use of lifecycle functions #151

Closed joejackson3 closed 7 months ago

joejackson3 commented 7 months ago

This is a very promising library. So far, I haven't been able to figure out how to use it for my Kotlin Multiplatform project. My only real requirement is to run a OnResume function to update my main activity, when it is brought back into focus from background.

I am not clear where to instantiate the functions:

val lifecycleRegistry = LifecycleRegistry()
val someLogic = SomeLogic(lifecycleRegistry)

Do I need to call lifecycleRegistry.subscribe() from the Composable of the activity in question?

What I'm currently doing in my main module is define the SomeLogic class:

class SomeLogic(lifecycle: Lifecycle,t:MutableState<String>) {
    init {
        lifecycle.subscribe(
            onResume = {t.value="Res1"}
        )
        lifecycle.doOnResume {
             t.value = "Res";
        }
    }
}

then instantiate the members in the Composable of the main activity: The OnResume function is however never called.

val lifecycleRegistry = LifecycleRegistry()
val someLogic = SomeLogic(lifecycleRegistry,t3)
lifecycleRegistry.subscribe()
lifecycleRegistry.doOnCreate { t3.value="yo" }

The OnResume function is however never called. Any advice or reference to sample projects would be highly appreciated.