Circular Reference with Nested Nav Graphs

Viewed 822

I have a complex navigation structure, I go from one nav graph then based on some logic, decide which nav graph to go to there. However both the nav graphs share 3 other nav graphs. See image below

Example

Currently I get a circular reference error if I try to include each nav graph in the A & B. I have also tried creating a global action, which leads to illegal argument exception since it doesn't exist on the nav graph.

Please help!!!

1 Answers

I also don't know how to solve this. The Navigation Component should allow the reuse of modular graphs by passing some sort of ID from the initiating graph.

The only workaround I could think of was to create a copy of the graphs that have different origins.

For example, "Graph1" would now have two versions, "Graph1_from_GraphA" and "Graph1_from_GraphB". So "GraphA" and "GraphB" would reference them respectively.

If the graphs are not that far way, there is also the solution of conditional navigation, as described in the official documentation (https://developer.android.com/guide/navigation/navigation-conditional), in which we can pop the BackStack in the NavController with a result in the SavedStateHandle and then in the previous fragment we can redirect to the desired destination.

Related