I’m Looking at refactoring our navigation compose ...
# compose
j
I’m Looking at refactoring our navigation compose NavGraph to include nested navigation. What is the recommendation for having a destination that is used in multiple nested graphs? Should those be different navigation routes or is it OK to use the same route within multiple NavGraphs?
s
Pretty sure accompanist https://github.com/android/nowinandroid/tree/main/app/src/main/java/com/google/samples/apps/nowinandroid/navigation does this, if you can take a look there. Some graphs provide also a “nested routes” parameter. Maybe it’s what you’re after, maybe not, not 100% sure
j
nice, thanks! I am moving towards a similar model of building the nav graph, but I think I want to do something different than this example. From what I understand from reading the docs, navigating to the Topic nested destination in that app will always put the start destination (interests) on the backstack. I need a destination that I go to from multiple nested graphs, but I don’t want it’s attached start destination to come with it… If that makes sense.
So I want something like this:
Copy code
navigation1() {
   destinationA()
   destinationB()
}
navigation2() {
   destinationC()
   destinationB()
}
Where destination B is in both graphs. I’m thinking I should make these separate routes that just use the same composable, but wanted to check if that was the best way to go or if there was a way to share a destination between graphs
s
Yes, then tivi is the sample to look at. https://github.com/chrisbanes/tivi/blob/main/app/src/main/java/app/tivi/AppNavigation.kt Here there’s multiple “versions” of some destinations in different parts of the navigation structure. Check it out and lmk if this is it
j
ah yeah, that is pretty much it. So that is scoping the nested destinations to the root that they are relative to which results in a unique route for that screen between it’s different nested graph usages.
which makes sense
thanks for the great examples!