Jasmin Fajkic
10/12/2022, 5:48 PMcomposable
and bottomSheet
( from accompanist ). In my composable route i set custom BackHandler. But when i navigate from that screen to bottom sheet screen , my bottom sheet screen inherit back handler behaviour from previous screen. Any chance to solve it 😞 ?Ian Lake
10/12/2022, 7:41 PMcomposable
destination is still visible underneath the bottom sheet right? Which would mean that the BackHandler
is still part of composition and still receiving back events unless you make it not enabled (i.e., pass some state that is false when you construct the BackHandler
)Jasmin Fajkic
10/12/2022, 7:42 PMval observer = remember {
LifecycleEventObserver { _, event ->
if (event.name == Lifecycle.Event.ON_STOP.name) {
onSheetClose()
}
}
}
val lifecycle = LocalLifecycleOwner.current.lifecycle
DisposableEffect(lifecycle, observer) {
lifecycle.addObserver(observer)
onDispose { lifecycle.removeObserver(observer) }
}
Ian Lake
10/12/2022, 7:42 PMSTARTED
, instead of RESUMED
. You could use that as a signal for if your BackHandler
should be enabled or notIan Lake
10/12/2022, 7:44 PMBackHandler
if it isn't supposed to intercept back - that way the default NavController one will pick it up (which is important since that is going to matter for running animations)Ian Lake
10/12/2022, 7:47 PMval lifecycle = LocalLifecycleOwner.current.lifecycle
val currentLifecycleState = remember(lifecycle) { mutableStateOf(lifecycle.currentState) }
// Do a DisposableEffect to update currentLifecycleState
BackHandler(currentLifecycleState.isStateAtLeast(Lifecycle.State.RESUMED) {
// Your back logic
}
Jasmin Fajkic
10/12/2022, 7:47 PMIan Lake
10/12/2022, 7:50 PMIan Lake
10/12/2022, 7:51 PMLifecycle