Sandeep Dhami
06/24/2025, 10:24 AMNavHost
with an independent NavController
.
For example, our main NavHost
might show RootScreenA
, which then composes a FeatureANavHost
(with featureANavController
) internally. Similarly, RootScreenB
might compose FeatureBNavHost
(with featureBNavController
).
The challenge we're facing is how to achieve coordination or navigation between these separate NavController
instances. For instance, if an action occurs deep within `FeatureANavHost`'s stack, we might need to navigate to a specific screen within `FeatureBNavHost`'s stack.
Currently, we're exploring methods like using a global event bus/coordinator (e.g., SharedFlow
or a simple object with callbacks accessible at a higher level) to "signal" navigation events from one independent `NavController`'s domain to another. The coordinator then explicitly calls navigate()
on the target NavController
.
While this works, it feels like we're working against the core principles of Jetpack Compose Navigation, which typically advocates for a single NavController
for the entire app, leveraging nested navigation { ... }
blocks.
My questions are:
1. Is this pattern of having multiple independent `NavHost`es (each with its own NavController
) considered an anti-pattern for a single-activity app, even for modularity in a deep tree structure?
2. If it is, what are the recommended patterns or best practices to achieve similar modularity and hierarchical organization (especially with deep nesting like 10+ levels) while sticking to a single NavController
?
3. If this multi-NavController
approach can be viable under specific circumstances, what are the most robust ways to handle cross-controller "navigation" or coordination, and what are the major pitfalls to watch out for beyond basic isolation?
Any insights, examples, or pointers to relevant architectural discussions would be greatly appreciated! Thanks in advance!Debayan
07/15/2025, 12:50 PMSandeep Dhami
07/15/2025, 12:52 PMDebayan
07/15/2025, 2:05 PMSandeep Dhami
07/15/2025, 3:42 PMSandeep Dhami
07/15/2025, 3:48 PMSandeep Dhami
07/17/2025, 8:52 AMOkay, but if there are 3 screens A B C hosted on the RootScreen all three of them have their own nav controller. And lets say there is D screen which is a child of C. Then how D is opened via deeplink? As D is having its own graph which is completly unaware of the the RootScreen or RootNavController?@Debayan can you help me on this