alaershov
02/27/2024, 12:21 PMModalBottomSheet
has SheetState, which has confirmValueChange - a callback that I use to forbid ModalBottomSheet
dismiss depending on its logical state. This works fine with "tap outside sheet" and "drag sheet down" dismiss actions. However, a system Back button press does not respect confirmValueChange
- it just dismisses the sheet without any hesitation.
There are ModalBottomSheetProperties, such as shouldDismissOnBackPress
. Unfortunately, setting shouldDismissOnBackPress=false
just disables the back button completely - the sheet's Window intercepts it, but does nothing.
BackHandler
also doesn't work, as it requires underlying support in the implementation of Window. Dialog
composable has this support, but not ModalBottomSheet
.
Basically, I want everything to work exaclty as it does, except make back button press respect confirmValueChange
. I even tried copying MaterialBottomSheet sources to modify this behavior, but it's a banana-monkey-jungle problem, too much stuff is internal
.
Any advice on how to make this work? Thanks in advance!Joel Denke
02/27/2024, 12:25 PMalaershov
02/27/2024, 12:28 PMJoel Denke
02/27/2024, 12:30 PMalaershov
02/27/2024, 12:31 PMJoel Denke
02/27/2024, 12:32 PMBox {
BackHandler()
ModalBottomSheet()
}
alaershov
02/27/2024, 12:33 PMJoel Denke
02/27/2024, 12:35 PMval sheetState = rememberModalBottomSheetState(skipPartiallyExpanded = true)
val coroutineScope = rememberCoroutineScope()
BackHandler(enabled = sheetState.isVisible) {
coroutineScope
.launch { sheetState.hide() } // Ignore in your case
.invokeOnCompletion {
if (!sheetState.isVisible) {
// do something
}
}
}
ModalBottomSheet(
modifier = Modifier.fillMaxWidth(),
content = {
content(model)
},
sheetState = sheetState,
onDismissRequest = { navigator.finish(onDismiss()) },
)
Joel Denke
02/27/2024, 12:35 PMalaershov
02/27/2024, 12:36 PMJoel Denke
02/27/2024, 12:37 PMalaershov
02/27/2024, 12:37 PMNot sure what you mean with WIndowMaterial 3 Bottom Sheet is implemented internally as a ModalBottomSheetWindow. It handles back press, because if you have more than one open Window, the back press is dispatched to the topmost one, which is ModalBottomSheetWindow
Joel Denke
02/27/2024, 12:38 PMalaershov
02/27/2024, 12:38 PMYeah it workstry removing everything inside your BackHandler - I bet back button will still close your bottom sheet
alaershov
02/27/2024, 12:39 PMJoel Denke
02/27/2024, 12:39 PMJoel Denke
02/27/2024, 12:40 PMalaershov
02/27/2024, 12:42 PMJoel Denke
02/27/2024, 12:44 PMalaershov
02/27/2024, 12:44 PMJoel Denke
02/27/2024, 12:44 PMJoel Denke
02/27/2024, 12:45 PMproperties = ModalBottomSheetDefaults.properties(shouldDismissOnBackPress = false)
ignores back press on bottomsheet.
WIll now try combine everything to see how behaves.Joel Denke
02/27/2024, 12:45 PMalaershov
02/27/2024, 12:45 PMalaershov
02/27/2024, 12:46 PMalaershov
02/27/2024, 12:50 PMJoel Denke
02/27/2024, 12:53 PMalaershov
02/27/2024, 12:56 PMJoel Denke
02/27/2024, 1:11 PMJoel Denke
02/27/2024, 1:12 PMalaershov
02/27/2024, 1:14 PMJoel Denke
02/27/2024, 1:14 PMoverride fun onAttachedToWindow() {
super.onAttachedToWindow()
maybeRegisterBackCallback()
}
override fun onDetachedFromWindow() {
super.onDetachedFromWindow()
maybeUnregisterBackCallback()
}
private fun maybeRegisterBackCallback() {
if (!properties.shouldDismissOnBackPress || Build.VERSION.SDK_INT < 33) {
return
}
if (backCallback == null) {
backCallback = Api33Impl.createBackCallback(onDismissRequest)
}
Api33Impl.maybeRegisterBackCallback(this, backCallback)
}
private fun maybeUnregisterBackCallback() {
if (Build.VERSION.SDK_INT >= 33) {
Api33Impl.maybeUnregisterBackCallback(this, backCallback)
}
backCallback = null
}
This wants me go hide and never look at this code again ...Stylianos Gakis
02/27/2024, 2:53 PMenableOnBackInvokedCallback
on your manifest and not the latest material3 library and are hitting this https://issuetracker.google.com/issues/281967264?alaershov
02/27/2024, 3:09 PMAlex Vanyo
02/27/2024, 4:46 PMJoel Denke
02/27/2024, 4:51 PMalaershov
02/28/2024, 5:30 AMalaershov
02/28/2024, 5:32 AMalaershov
02/28/2024, 11:20 AMJacob Rhoda
04/19/2024, 5:33 PMalaershov
05/07/2024, 5:41 AM