Thread
#compose
    b

    Berkeli Alashov

    1 year ago
    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).
    e

    eygraber

    1 year ago
    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

    Berkeli Alashov

    1 year ago
    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

    eygraber

    1 year ago
    I meant something along the lines of setting
    enabled = false
    on the inner ones when you want to outer one to have control
    b

    Berkeli Alashov

    1 year ago
    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

    Ian Lake

    1 year ago
    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
    I.e.,
    if (expanded)
      BackHandler {
        // Collapse
      }
    }
    m

    ms

    1 year ago
    @Berkeli Alashov if HalfExpanded is your problem you can use
    animateTo(Expanded)
    which is what expand() uses under the hood
    b

    Berkeli Alashov

    1 year ago
    I didn't know about the "last added wins" part. I will try adding it when it's expanded
    e

    eygraber

    1 year ago
    @Ian Lake last added in the context of time or depth of the tree?
    i

    Ian Lake

    1 year ago
    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

    Berkeli Alashov

    1 year ago
    Thanks @Ian Lake, worked like a charm.
    e

    eygraber

    1 year ago
    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

    Ian Lake

    1 year ago
    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 😄
    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

    eygraber

    1 year ago
    Because it's happening in recomposition?
    i

    Ian Lake

    1 year ago
    Because it is happening from a user's action, which is by necessity after the first composition completes
    e

    eygraber

    1 year ago
    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

    Ian Lake

    1 year ago
    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
    jossiwolf

    jossiwolf

    1 year ago
    Yeah, +1 on Accompanist Navigation Material 🙂 Shouldn't be long!