Home > database >  BottomNavigationView with nested graph
BottomNavigationView with nested graph

Time:05-04

I have a problem in my application when using nested graph in my nave graph xml file It's my nav_graph file

    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@ id/nav_graph"
    app:startDestination="@navigation/nav_entries">

    <include app:graph="@navigation/nav_entries"/>
    <include app:graph="@navigation/nav_states"/>
    <include app:graph="@navigation/nav_calendar"/>
    <include app:graph="@navigation/nav_more"/>

</navigation>

and this is fragment container view in activity_main

 <androidx.fragment.app.FragmentContainerView
        android:id="@ id/fragmentContainerView"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:navGraph="@navigation/nav_graph" />

And this is code from main activity to setup bottom navigation view with nave controller

 private fun setupUi() {
        val navHostFragment = supportFragmentManager.findFragmentById(R.id.fragmentContainerView) as NavHostFragment
        navController = navHostFragment.navController()
        binding.bottomNavigationView.setupWithNavController(navController)
    }

But when I run the app, It will crash and this is error :

navigation destination com.iranmobiledev.moodino:navigation/nav_entries is not a direct child of this NavGraph

Help me to fix.

CodePudding user response:

It's incorrect. In navigation, not all nested navigation graphs should be defined with include, and a node(fragment) should be defined as the start destination. You can define other fragments in the nested navigation graph and use it with the include tag. Use this code:

app:startDestination="@id/first_fragment_id">

Instead of:

app:startDestination="@navigation/nav_entries">
  • Related