<@UHAJKUSTU> how can i trigger the back intercepto...
# decompose
v
@Arkadii Ivanov how can i trigger the back interceptor when i navigate back manually by calling
navigator.pop()
from the app bar button or so When the BackHandler is added by some specific component in the stack nav Eg:
Copy code
--- Root Component (Stack Nav) (Has the app bar with back button)
------- Screen 1 (Adds back handler)
a
It's not possible. BackHandler is not used when you navigate manually. There are two alternatives. 1. You can bubble your Back request up to the root level and call the back dispatcher. E.g. call OnBackPressedDispatcher#onBackPressed on Android, or BackDispatcher on all other platforms. 2. You can try creating a custom ComponentContext that would have a back() method in it, and that would call the root back dispatcher for you. I never tried this option, but I guess it should be possible.
v
Any possible way to get all the back handlers attached to the root context based on tag or some field?
a
I'm not sure I understand what you mean.
All BackHandlers are somewhat attached to the root.
v
Anyway to get a list of all?
Or find a back handler?
What i was thinking was to keep a back handler inside the "BaseComponent" class i have and invoke the back handler when app bar back button is pressed instead of just
navigator.pop()
Would this work?
a
I don't think it would work. Each child ComponentContext has its own BackHandler, attached to the parent ComponentContext. So there is a tree of BackHandlers.
v
It did work
Copy code
childStack.value.active.instance.component.backCallback?.onBack() ?: navigation.pop()
Calling this on app bar back press If any component in stack nav has a custom back handler, it will be invoked
a
This assumes that the backCallback is the only callback in the child component. There could be another BackCallback somewhere in the sub-tree.
But glad if it works for your case.
v
yeah, if there is any more back callbacks, deeper in the tree It wont work