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
Zach Klippenstein (he/him) [MOD]
04/30/2021, 4:45 PM
BackHandler
with an event handler that pops your backstack
👍 2
r
robnik
04/30/2021, 4:57 PM
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
Zach Klippenstein (he/him) [MOD]
04/30/2021, 5:36 PM
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
Ian Lake
04/30/2021, 7:15 PM
Actually, you'd want to use the
enabled
parameter of
BackHandler
for the stack size, not just conditionally including the
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