Kenneth Leong
06/10/2024, 2:41 PMBackHandler
before i invoke`doSumthing()` .
// this is inside my composable
val enabledBackHandler = remember { mutableStateOf(true) }
BackHandler(enableBackHandler.value) {
enabledBackHandler.value = false
doSumthing()
}
// this is a hoisted lambda Function, in my fragment
fun doSumthing() {
requireActivity().backPressDispatcher.onBackPress()
}
Stylianos Gakis
06/10/2024, 3:00 PMbackPressDispatcher.onBackPress()
looks super suspicious, what are you trying to do?Kenneth Leong
06/10/2024, 3:31 PMsuper.onbackPress()
. My backhandler has a when statement where only on certain UI states it does sumthing in BackHandler()
, and on certian UI states it juz lets the framework do its onbackpressDispatcher.onbackpressed()
.Stylianos Gakis
06/10/2024, 3:33 PMBackHandler(here)
instead then.
Not having a Backhandler (or having it turned off) is the right way to let the system do its own thing.Stylianos Gakis
06/10/2024, 3:34 PMBackHandler(uiState is UiState.SpecialBackHandlingCase) {
Do custom thing()
}
Kenneth Leong
06/10/2024, 3:37 PMif(!composeNavHostController.navigateUp()) {
// this shld actually juz do a navController.popBackStack()
requireActivity().backPressDispatcher.onBackPress()
}
My code is like this as i am actually using a compose navigation in my fragment with its own navgraph. and my fragment is part of the overall apps navcontroller. so essentially its a compose navigation in a fragment, with the fragment as part of a larger navgraph.Stylianos Gakis
06/10/2024, 3:39 PMnavigateUp()
from a back press in the first place either. What are you trying to achieve with that there?Stylianos Gakis
06/10/2024, 3:39 PMKenneth Leong
06/10/2024, 3:40 PMKenneth Leong
06/10/2024, 3:40 PMKenneth Leong
06/10/2024, 3:41 PMKenneth Leong
06/10/2024, 3:41 PMKenneth Leong
06/10/2024, 3:42 PMStylianos Gakis
06/10/2024, 3:43 PMStylianos Gakis
06/10/2024, 3:44 PMStylianos Gakis
06/10/2024, 3:44 PMKenneth Leong
06/10/2024, 3:50 PMKenneth Leong
06/10/2024, 3:51 PMStylianos Gakis
06/10/2024, 3:55 PMthe problem with the current default system backpress is that is does popBackStack on the app's navcontroller navgraph, which pops a fragment. And not the compose's navHostController's which pops a composable.So you got a NavHost on your screen, yet the back is consumed by the fragment above it all? NavHost includes a BackHandler internally, I am not sure this is expected here.
i use backHandler to block user's backpress in the case where my composable is in a loading stateI am gonna be honest and say that this feels like a very frustrating experience, why do you do that?
and also to handle back press to display a compose dialog for user confirmation.Does compose's
Dialog()
not do that for you automatically as well?Kenneth Leong
06/10/2024, 4:00 PMStylianos Gakis
06/10/2024, 4:02 PMKenneth Leong
06/10/2024, 4:04 PMStylianos Gakis
06/10/2024, 10:08 PM