What's the latest way to hook into the "back" butt...
# compose
r
What's the latest way to hook into the "back" button? I see some older posts but they seem outdated. I'm experimenting with writing a custom nav controller, since the official NavController does not do what I want. It seems relatively easy thanks to Compose, but the back button is a question mark... maybe more difficultly is hiding there.
z
BackHandler
with an event handler that pops your backstack
👍 2
r
Okay, I found
BackHandler
. Perfect, thanks! By "backstack" do you mean something in the system API that I need to hook into, or do you just mean whatever stack/nav system I'm implementing? I'm clear how to implement my version of "back", but I'm not sure how to know when the user intends to go "back" out of my app.
z
Yea whatever your nav system is. If you don’t specify a back handler then back will back out of your app, so just don’t call
BackHandler
if your stack is size 1
i
Actually, you'd want to use the
enabled
parameter of
BackHandler
for the stack size, not just conditionally including the
BackHandler
. As per the custom back docs (https://developer.android.com/guide/navigation/navigation-custom-back), the ordering of `BackHandler`s is important for dispatch - you'd want to maintain the correct place in the list as it were compared to parent, sibling, and child
BackHandler
instances
🙏 1
today i learned 1
Conditionally adding a
BackHandler
does have its use cases (say, for a side nav drawer that needs to be at the top of the dispatch queue when it is open, no matter what `BackHandler`s the underlying content have), it just isn't the right one for the back stack size case