google / accompanist

A collection of extension libraries for Jetpack Compose
https://google.github.io/accompanist
Apache License 2.0
7.42k stars 597 forks source link

[Navigation Material] onResume/onPause lifecycle events are not received for bottomSheet destination #1725

Closed sbernikov closed 11 months ago

sbernikov commented 11 months ago

Description I have bottomSheet destination in my project and appropriate composable function that displays its content. Inside this function I listen to lifecycle events via DisposableEffect:

@Composable
fun ObserveLifecycleEvents(
    lifecycleOwner: LifecycleOwner = LocalLifecycleOwner.current,
    onStart: (() -> Unit)? = null,
    onResume: (() -> Unit)? = null,
    onPause: (() -> Unit)? = null,
    onStop: (() -> Unit)? = null,
) {
    DisposableEffect(lifecycleOwner) {
        val observer = LifecycleEventObserver { _, event ->
            when (event) {
                Lifecycle.Event.ON_START -> onStart?.invoke()
                Lifecycle.Event.ON_RESUME -> onResume?.invoke()
                Lifecycle.Event.ON_PAUSE -> onPause?.invoke()
                Lifecycle.Event.ON_STOP -> onStop?.invoke()
                else -> {}
            }
        }
        lifecycleOwner.lifecycle.addObserver(observer)
        onDispose { lifecycleOwner.lifecycle.removeObserver(observer) }
    }
}

But onResume/onPause events are not received if I move app to background, for example. Only onStart/onStop events working.

Steps to reproduce

Expected behavior onResume/onPause events are received when app goes to background

Additional context accompanist version = "0.32.0"

sbernikov commented 11 months ago

ok, so I was using confirmValueChange = { false } which is wrong for bottomSheet destination and may result navigator be out of sync