https://kotlinlang.org logo
#compose-android
Title
# compose-android
b

bryankeltonadams

10/10/2023, 7:58 PM
Has anyone had any luck with having a Material3 ModalBottomSheet backHandler work? I see there's an existing issue. https://issuetracker.google.com/issues/271303558 Just wondering if anyone has found a workaround or any particular way of implementing a backHandler to get it working. I'm basically not wanting the back button to hide the ModalBottomSheet but it does no matter what.
m

Mahmoo

10/10/2023, 9:57 PM
you have modalBottomSheetState right? you can use the state to hide it.. Also if you want that when system back button is pressed then use back handler
b

bryankeltonadams

10/10/2023, 10:16 PM
I don't have any issues using the state to hide it, the issue is that my ModalBottomSheet has navigation inside of it and if I hit the backButton on a screen that I navigate to inside of the sheet, it just closes the sheet instead of navigating back. I've tried putting a backHandler inside of the ModalBottomSheet and right outside of it and neither get triggered. The backHandler worked fine when I was using a ModalBottomSheetLayout with Material but since I've migrated to Material3 I have not figured out how to do any sort of back handling with the ModalBottomSheet.
m

Mahmoo

10/10/2023, 11:44 PM
Can you try this? Keep the body of your backHandler empty... this should help
try by keeping the backHandler empty
z

Zoltan Demant

10/11/2023, 3:15 AM
Are you using
android:enableOnBackInvokedCallback="true"
? The sheet intercepts key events, and with this enabled the "new" back event is ignored (the sheet just takes the old event into account). Disabling it should allow you to use BackHandler to do anything you like.
a

ascii

10/11/2023, 3:31 AM
Because of https://kotlinlang.slack.com/archives/C04TPPEQKEJ/p1695306835513579?thread_ts=1695298775.663439&cid=C04TPPEQKEJ, I believe custom BackHandlers cannot override M3 sheet's default back handling. You'll have to put your navigation logic in ModalBottomSheet's
onDismissRequest
, because that is called when you press back (or tap on the scrim etc).
So you could check nav back stack in that callback, and choose to not hide the sheet. I'm not a 100% sure because I haven't done this specifically myself.
b

bryankeltonadams

10/11/2023, 3:39 PM
Zoltan, I don't believe I have this enabled, is it enabled if it isn't disabled though?
z

Zoltan Demant

10/11/2023, 3:41 PM
I'm 99% on it being opt in rather than opt out!
b

bryankeltonadams

10/11/2023, 3:44 PM
Okay, yeah I for sure don't have this in my manifest and I put a BackHandler outside of the Sheet and inside as well.
Adhiraj, if I put something in my onDismissRequest, the sheet will still be put into a hidden state when the backButton is pressed, right? But then in my onDismissRequest I'd have to check if the screen I am on is not the first screen in my navigation, reshow the sheet and popBackStack? But then there'd be weird visual behavior of pressing the back button would make the sheet close, open, and then navigate to where it's supposed to go.
a

ascii

10/11/2023, 3:48 PM
It's been a while since I worked with M3 sheets and that's the part I'm not sure about. Perhaps overriding
confirmStateChange
(or whatever it's called) along with
onDismissRequest
will get you what you want?
So if nav has a back stack, confirmStateChange should return false if it's transitioning from Expanded -> Hidden, true otherwise
Test it and see if it behaves like I think it would
You'd override confirm in
rememberModalBottomSheetState
b

bryankeltonadams

10/11/2023, 3:52 PM
I tried just immediately returning false in the function and it doesn't do anything.
Even with removing onDismissRequest, all that happens is false is returned when the function is called but the sheet still slides down into a Hidden state.
egg.webm
2 Views