adrielcafe / voyager

🛸 A pragmatic navigation library for Jetpack Compose
https://voyager.adriel.cafe
MIT License
2.55k stars 130 forks source link

Working with lifecycle events in common #339

Open mainrs opened 7 months ago

mainrs commented 7 months ago

I want to prevent flow collection on recomposition. Currently, I am using this code:

@Composable
fun UI() {
    val screenModel: ScreenModel = ...

    Column { ... }

    LaunchedEffect(Unit) {
         screenModel.effect.collectLatest { ... }
    }
}

However, this adds the collector again once recomposition happens due to lifecycle changes like PAUSE or RESUME on Android. How can I tackle this problem properly using Voyager? There only seems to be the possibility to listen for onStarted and onDisposed events and nothing more using LifecycleEffect. Is there a reason why the other events are missing?

mainrs commented 7 months ago

https://github.com/adrielcafe/voyager/issues/225#issuecomment-1952346970

Kashif-E commented 7 months ago

@mainrs https://proandroiddev.com/tracking-lifecycle-changes-in-compose-multiplatform-ios-and-android-486005919ec8

hoc081098 commented 7 months ago

Try this https://github.com/hoc081098/solivagant/tree/master/lifecycle

DevSrSouza commented 7 months ago

Google and Jetbrains are working on Lifecycle multiplatform, there are already merged changes for that, I assume it will be available until the final 1.6.0 release for Compose KMP. As for now, we need to wait or use a third party solution.

When this API be available in Compose rc versions, we should update Voyager to support it here.

https://developer.android.com/jetpack/androidx/releases/lifecycle#2.8.0-alpha02 https://github.com/JetBrains/compose-multiplatform-core/pull/1043

wiryadev commented 5 months ago

Is this why collectAsStateWithLifecycle is crash (cant find lifeycleOwner) even when being run on Android device?

wiryadev commented 5 months ago

When this API be available in Compose rc versions, we should update Voyager to support it here.

It is now in RC: https://github.com/JetBrains/compose-multiplatform/releases/tag/v1.6.10-rc01