0

I am trying to build a navGraph programmatically and link it to a bottomNavigationView. I have to do it programmatically because I received the order of items and if they are visible from server.

If i create a navGraph using xml, it works, but when i try to build the navGraph programmatically it fails, this is the navGraph in xml:

    <?xml version="1.0" encoding="utf-8"?>
    <navigation xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/dummy_graph"
        app:startDestination="@+id/home_nav_graph">
        
        <include app:graph="@navigation/home_nav_graph" />
        <include app:graph="@navigation/guide_nav_graph" />
        <include app:graph="@navigation/recordings_nav_graph" />
        
    </navigation>

If i use this navGraph xml, it works, but if try to create it using navController.createGraph it crashes since it cannot find the start destination:

     val navHostFragment = supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
      val navController = navHostFragment.navController
      navController.graph = navController.createGraph(
             startDestination = R.id.home_nav_graph
      ) {
                R.id.home_nav_graph
                R.id.guide_nav_graph
                R.id.recordings_nav_graph
      }
    
      findViewById<BottomNavigationView(R.id.bottom_navigation)
           .setupWithNavController(navController)
```
The app crashes: java.lang.IllegalArgumentException: navigation destination 2131231236 is not a direct child of this NavGraph

Is there a way to create a dynamic navGraph that includes other navGraphs inside?

Alex Vega
  • 51
  • 5

0 Answers0