We have the same problem like this one in <stackov...
# compose-android
g
We have the same problem like this one in stackoverflow. We can't disable buck button in ModalBottomSheet. Any idea? We already tried the
BackHandler
and dialog properties:
Copy code
dialogProperties = DialogProperties(
            usePlatformDefaultWidth = false,
            dismissOnBackPress = false,
            dismissOnClickOutside = false
        )
Any idea? 🙄
b
same issue, not trying to disable back button, but just change it's behavior. Have you found anything?
o
I have been investigating it a little bit, and as I understand it is not possible as for now. The underlying implementation uses
ModalBottomSheetWindow
, which has this code to handle back press:
Copy code
override fun dispatchKeyEvent(event: KeyEvent): Boolean {
        if (event.keyCode == KeyEvent.KEYCODE_BACK) {
            if (keyDispatcherState == null) {
                return super.dispatchKeyEvent(event)
            }
            if (event.action == KeyEvent.ACTION_DOWN && event.repeatCount == 0) {
                val state = keyDispatcherState
                state?.startTracking(event, this)
                return true
            } else if (event.action == KeyEvent.ACTION_UP) {
                val state = keyDispatcherState
                if (state != null && state.isTracking(event) && !event.isCanceled) {
                    onDismissRequest()
                    return true
                }
            }
        }
        return super.dispatchKeyEvent(event)
    }
So it always handles back button and invokes
onDismissRequest
, where it does the following:
Copy code
onDismissRequest = {
            if (sheetState.currentValue == Expanded && sheetState.hasPartiallyExpandedState) {
                scope.launch { sheetState.partialExpand() }
            } else { // Is expanded without collapsed state or is collapsed.
                scope.launch { sheetState.hide() }.invokeOnCompletion { onDismissRequest() }
            }
        },
Thus hides or partially collapses the modal bottom sheet and now there are no customization options 😕 I guess you can give a to this issue: https://issuetracker.google.com/issues/278216859
b
Oleksandr, I looked into the file containing the ModalBottomSheet and I speculated the same issue as you described, a lot of this code is internal to this file otherwise it might not be that awful to make a custom ModalBottomSheet Composable that rips out a lot of that backHandling stuff. i.e. fixing the issue ourselves on a per-project level.
sorry, not just internal to the file but also internal to the material3.compose package etc.