Is there a way to have a special `BackHandler` that consumes back calls before the inner most compos...
b
Is there a way to have a special
BackHandler
that consumes back calls before the inner most composable? My use case is I have a
BottomSheetScaffold
at the root of my app (Theme>BottomSheet>Home>NavHost) and I want to close the sheet on back clicks. But BackHandler defined in BottomSheet doesn't get invoked until all back handlers deeper in the tree are used (in my case, NavHost consumes before BottomSheet, until the first destination is reached / nav stack cleared).
1
e
There's probably a way to do this using the
enabled
parameter to
BackHandler
but it can probably get complex managing that across your tree
b
I'm already using
enabled
to only handle back clicks when the sheet is expanded. It's not checked before inner back handlers are used.
e
I meant something along the lines of setting
enabled = false
on the inner ones when you want to outer one to have control
b
Yeah but I can only do that to my own defined BackHandlers, not the ones defined by libraries like navigation
I saw this https://github.com/androidx/androidx/pull/173 and https://github.com/google/accompanist/pull/592, which seems to allow bottom sheets as navigation destinations. Although it's only for
ModalBottomSheetLayout
. I can't use it because it has a HalfExpanded state that afaik can't be disabled.
i
The correct answer is to only add your BackHandler on demand, not have it added all the time - the last added still wins, which is exactly what you need for an "overlay" style component that you want to dismiss
🙏 1
I.e.,
Copy code
if (expanded)
  BackHandler {
    // Collapse
  }
}
m
@Berkeli Alashov if HalfExpanded is your problem you can use
animateTo(Expanded)
which is what expand() uses under the hood
b
I didn't know about the "last added wins" part. I will try adding it when it's expanded
e
@Ian Lake last added in the context of time or depth of the tree?
i
The upcoming Accompanist Navigation Material only goes between expanded and collapsed, it never opens to only half expanded AFAIK
Last added in terms of time (it just happens to be that parents are evaluated before children Composables)
b
Thanks @Ian Lake, worked like a charm.
e
So if the
NavHost
is a child wouldn't that always get "added last" in this case (I guess not because OP said it worked, but I guess I'm missing something)
i
I'd give the Accompanist Navigation Material library a try when it comes out - the back button works exactly as you'd want it to out of the box 😄
1
It is more that by making it conditional, you only add it after that initial hierarchy is built out
I.e., when the sheet is opened
e
Because it's happening in recomposition?
i
Because it is happening from a user's action, which is by necessity after the first composition completes
e
Sounds like it could lead to subtle race conditions in more complex scenarios, but I haven't met a back handling solution that didn't have that issue 😅 Thanks!
i
Keeping things based solely on your hierarchy and using
enabled
is absolutely the best way to do things when they aren't a temporary overlay
👍 1
j
Yeah, +1 on Accompanist Navigation Material 🙂 Shouldn't be long!
201 Views