Skip to content Skip to sidebar Skip to footer

Null NavHostFragment/NavController With FragmentContainerView

Expected Create bottom app bar navigation using Navigation UI's BottomNavigationView and FragmentContainerView similar to the sample app NavigationAdavancedSample. Note, Navigatio

Solution 1:

I also had this problem. My mistake was that I did not enter the correct ID for navigation tag in navigation files. Note that the ID must be equal to the ID of the menu items.

in navigation file (navigation/home.xml):

<navigation
android:id="@+id/home" ... >

and in menu file (menu/bottom_nav.xml):

 <item
    android:id="@+id/home" ... />

Solution 2:

Add a parent navigation graph for the BottomNavigationView menu items.

The full sample code is included within BottomNavigationSample.

  1. Create a parent level navigation graph for the menu fragments. Note, this is not reflected in the NavigationAdvancedSample architecture pattern which has three separate navigation graphs without a parent navigation graph.

This can be accomplished with one parent navigation graph and fragments or nested navigation graphs for each menu item. Nested navigation graphs are good because each sub-flow can be organized within the nested graph.

main.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/main"
    app:startDestination="@id/home">
    <include app:graph="@navigation/home" />
    <include app:graph="@navigation/saved" />
</navigation>
  1. Add the parent graph using navGraph in the FragmentContainerView.

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/nav_host_container"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        app:defaultNavHost="true"
        app:navGraph="@navigation/main"/>

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottom_nav"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:menu="@menu/bottom_nav" />

</LinearLayout>
  1. Enable the BottomNavigationView in MainActivity.kt.
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.setupActionBarWithNavController
import androidx.navigation.ui.setupWithNavController
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val appBarConfiguration = AppBarConfiguration(setOf(R.id.home, R.id.saved))

        val navHostFragment = supportFragmentManager.findFragmentById(R.id.nav_host_container) as NavHostFragment
        val navController = navHostFragment.navController

        setupActionBarWithNavController(navController, appBarConfiguration)
        bottom_nav.setupWithNavController(navController)
    }
}

Post a Comment for "Null NavHostFragment/NavController With FragmentContainerView"