Android Jetpack Navigation in multi modular apps

Demo
Nested graph Preview

nav_graph_app_module is the app module’s graph
We will reference this graph in our activity_main.xml , fragment container
We include nav_graph_home_host here as it is another module and to navigate to that module we simply navigate to nav_graph_home_host graph

Navigate from splash to homefindNavController().navigate(R.id.nav_graph_home_host)

From splash we will navigate to nav_graph_home_host graph,
this graph contains a fragment HomeHostFragment
HomeHostFragment contains an bottom nav view and a fragment container
as we are dealing with nested fragment so to avoid stack overflow issue
and also to keep things simple
we will create another graph nav_graph_home containing all other fragments we need in this module

nav_graph_home includes multiple graph from different modules
Each graph is associated with menu click of bottom nav view
To achieve navigation we will keep bottom navigation id same as graph’s id
check the Github source code for complete insight

bottonNavView.setupWithNavController(navController)

Now stack is like ::
splash → home
(home will not maintain any stack as navHost is set to default i.e., false)

Now if we want to navigate from home to other fragment
but wanted to host from our root nav controller, then can use the below function

/*
`R.id.fragment_container_root` is id of our root fragment
which we inflated in Main Activity
*/
val Fragment.findRootNavHost: NavController?
get() = this.activity?.let {
Navigation.findNavController(it, R.id.fragment_container_root)
}

In fragment from where we want to navigate

findRootNavHost?.navigate(`your_destination_fragment`)

I’d like to end this post by saying thank you 🙏 for reading
If have any query we can connect, links in my profile or you can comment it down

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store