I have an unexpected (to me at least) behaviour wi...
# compose
j
I have an unexpected (to me at least) behaviour with
OnDestinationChangedListener
of the Navigation component. I have a bottom nav bar with 3 items, and the third is a subgraph. When I navigate to that one my OnDestinationChangedListener gets called twice, first with the start destination and then with the actual destination. Is this the intended behaviour?
✔️ 1
Also, what I am trying to achieve, in case someone has a better solution: When I click on one of the bottom items I save the route of destination in an observable so that I can use it to know which item should be highlighted. This does not work when using the system back, so I added a
OnDestinationChangedListener
to detect when the start destination was opened and update the observable with the start destination. As the
OnDestinationChangedListener
goes through the start destination, it breaks my solution.
i
You shouldn't be directly using an
OnDestinationChangedListener
at all within Compose, use the
currentBackStackEntryAsState()
as the documentation does: https://developer.android.com/jetpack/compose/navigation#bottom-nav
If you're using the
popUpTo
style shown there, you won't get any intermediate state such as the start destination being dispatched like you would if you did it with a separate
popBackStack()
followed by a
navigate()
j
Thank you, it seems to work! How are you so fast?? Also, is this new or sort of new? I diverged from the implementation of the documentation because I would get 2 start destinations on the backstack, and another issue I do not remember right now
i
The
popUpTo
stuff came in
alpha02
back in November, fixes for nested graphs came in
alpha03
in December, so not that recent, no
👍 1