Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lifecycle onPause/onStop/onDestory are never called #42

Closed
Syer10 opened this issue Jan 13, 2022 · 6 comments · Fixed by #87
Closed

Lifecycle onPause/onStop/onDestory are never called #42

Syer10 opened this issue Jan 13, 2022 · 6 comments · Fixed by #87
Labels
bug Something isn't working

Comments

@Syer10
Copy link
Contributor

Syer10 commented Jan 13, 2022

While testing my app with Voyager I noticed some of my AndroidView states were never being saved. Investigating that lead me to my LifecycleObserver's never filling the bundle contained in my rememberSavable variable. I then logged the states that the lifecycle observer goes through and noticed that only onCreate/OnStart/OnResume are ever called, and never the last 3.

Here are the test composables I used

@Composable
private fun rememberLoggingLifecycleObserver(): LifecycleEventObserver {
    val bundle = rememberSaveable { Bundle() }
    return remember {
        LifecycleEventObserver { _, event ->
            when (event) {
                Lifecycle.Event.ON_CREATE -> {
                    logcat("Lifecycle") { "OnCreate" }
                    logcat("Lifecycle") { bundle.keySet().joinToString() }
                    bundle.clear()
                }
                Lifecycle.Event.ON_START -> logcat("Lifecycle") { "OnStart" }
                Lifecycle.Event.ON_RESUME -> logcat("Lifecycle") { "OnResume" }
                Lifecycle.Event.ON_PAUSE -> logcat("Lifecycle") { "OnPause" }
                Lifecycle.Event.ON_STOP -> {
                    logcat("Lifecycle") { "OnStop" }
                    bundle.putAll(bundleOf("Yes" to "1200"))
                }
                Lifecycle.Event.ON_DESTROY -> logcat("Lifecycle") { "OnDestroy" }
                else -> throw IllegalStateException()
            }
        }
    }
}
@Composable
fun SetupLoggerLifecycleObserver() {
    val loggerObserver = rememberLoggingLifecycleObserver()
    val lifecycle = LocalLifecycleOwner.current.lifecycle
    DisposableEffect(lifecycle) {
        lifecycle.addObserver(loggerObserver)
        onDispose {
            // lifecycle.removeObserver(loggerObserver)
        }
    }
}
@Syer10
Copy link
Contributor Author

Syer10 commented Jan 17, 2022

I was investigating this, and I think it may be because the lifecycle is not bound to the screen, but the activity. Even when I am using AndroidScreen screens, which I thought replaced the LocalLifecycleOwner composition local.

@Syer10
Copy link
Contributor Author

Syer10 commented Jan 28, 2022

I did some more investigating with it, and I tested providing the screen lifecycles to the content and noticed it was not working either. I found that the screens lifecycle never gets passed INITIALIZED. Here is the function I used to test it

@Composable
fun ScreenWithLifecycle(screen: Screen) {
    if (screen is AndroidScreen) {
        val lifecycleOwner by derivedStateOf { (screen.getLifecycleOwner() as AndroidScreenLifecycleOwner) }
        val lifecycleState by derivedStateOf { lifecycleOwner.lifecycle.currentState }
        LaunchedEffect(lifecycleState) {
            logcat("Lifecycle") { lifecycleState.name }
        }
        CompositionLocalProvider(
            LocalLifecycleOwner provides lifecycleOwner
        ) {
            screen.Content()
        }
    } else {
        screen.Content()
    }
}

@adrielcafe adrielcafe added the bug Something isn't working label Feb 23, 2022
@adrielcafe
Copy link
Owner

1.0.0-rc02 now provides a LocalLifecycleOwner and emits onPause/onStop/onDestroy states when using AndroidScreen.

I think it isn't enough yet. Seems I also need to provide a LocalSaveableStateRegistry but the Android implementation, DisposableSaveableStateRegistry, is internal.

I'm still looking for a way to provide that, but in the meantime, can you confirm if the last modification solves your issue?

@Syer10
Copy link
Contributor Author

Syer10 commented May 2, 2022

Hmm, I don't think its working. The lifercycle observer is still isnt saving data into the bundle.

@miszmaniac
Copy link

I've encountered this issue. And due to that I can't handle ExoPlayer release() correctly in screen, when app get's paused.
Any info about bugfix here?

@Syer10
Copy link
Contributor Author

Syer10 commented Nov 17, 2022

I have a PR to fix it #87.

The fix is also included in my fork of Voyager: https://github.com/Syer10/voyager

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants