I'm trying to make this BottomNavigationView work with navigation but is giving me head-aches.
I'll explain my setup :
this the fragment of my activity_home.xml
<fragment
android:id="@+id/nav_host_fragment"
android:layout_width="match_parent"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_height="match_parent"
android:layout_marginBottom="?attr/actionBarSize"
app:defaultNavHost="true"
app:navGraph="@navigation/nav_graph" />
Also I've tried with FragmentContainerView
but is not working neither.
The error is on the navigation/nav_graph.xml
as the error says :
Exception inflating package:navigation/nav_graph line 7
Where line 7 is :
<?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/nav_graph"
app:startDestination="@id/featureHomeNavGraph">
<include //Line 7
android:id="@+id/featureHomeNavGraph"
app:graph="@navigation/feature_home_nav_graph"/>
<include
android:id="@+id/featureFavouritesNavGraph"
app:graph="@navigation/feature_favourites_nav_graph" />
....
</navigation>
Then I've created different layouts for the navigations as :
feature_home_nav_graph.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"
app:startDestination="@id/homeFragment">
<fragment
android:id="@+id/homeFragment"
android:name="package.HomeFragment"
android:label="HomeFragment" />
</navigation>
And feature_favourites_nav_graph.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"
app:startDestination="@id/favouritesFragment">
<fragment
android:id="@+id/favouritesFragment"
android:name="package.FavouritesFragment"
android:label="FavouritesFragment" />
</navigation>
Then I think the problem also comes by the implementation on my HomeActivity.kt
where I have to setup the navigation stuff... I'm doing it like this :
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_home)
setSupportActionBar(findViewById(R.id.toolbar))
val navHostFragment = supportFragmentManager
.findFragmentById(R.id.nav_host_fragment) as NavHostFragment?
NavigationUI.setupWithNavController(
bottomNavigation,
navHostFragment!!.navController
)
}
Also I've tried the NavigationExtension
followed on this tutorial but it did not work neither.
Note
I'm using the same id on my items of bottom_menu.xml
than on the navigation graph id for the fragments.
I guess the feature_home_nav_graph doesn't have an id on the navigation tag?