Reputation: 81
I want use navigation component from jetpack in my app. I could find base examples work of nav component + bottom view navigation,below I showed how I implemented using these examples. But its works uncorrect for me - its recreate fragments when switching between tabs. But i found navigatiom extension from google https://github.com/android/architecture-components-samples/blob/master/NavigationAdvancedSample/app/src/main/java/com/example/android/navigationadvancedsample/NavigationExtensions.kt but its too doesnt works.I do everything according to the instructions on my activity:
private fun setupBottomNavigationBar() {
val bottomNavigationView = findViewById<BottomNavigationView>(R.id.nav_bottom)
val navGraphIds = listOf(
R.navigation.home_nav,
R.navigation.search_nav,
R.navigation.scheduled_nav,
R.navigation.profile_nav
)
val controller = bottomNavigationView.setupWithNavController(
navGraphIds,
supportFragmentManager,
R.id.nav_host_container,
intent
)
controller.observe(this, Observer { navController ->
setupActionBarWithNavController(navController)
})
currentNavController = controller
}
I call this function in onCreate (when savedInstanceState == null) and in onRestoreInstanceState my layout file :
<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=".app.ui.main.MainActivity">
<androidx.fragment.app.FragmentContainerView
android:id="@+id/nav_host_container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/nav_bottom"
app:layout_constraintBottom_toBottomOf="parent"
android:background="@drawable/back_bottom_navigation"
android:layout_alignParentBottom="true"
app:menu="@menu/bot_nav_menu"
app:labelVisibilityMode="unlabeled"
app:itemIconTint="@color/bot_navigation_items"
app:itemTextColor="@color/bot_navigation_items"
/>
</LinearLayout>
and this is exactly the same code as in the example
But this does not work, fragments are not displayed, and if setupItemReselected (graphIdToTagMap, fragmentManager) is installed in the Navigation Extention, an error is caused in this function on the line val selectedFragment = fragmentManager.findFragmentByTag (newlySelectedItemTag)
with error kotlin.TypeCastException: null cannot be cast to non-null type androidx.navigation.fragment.NavHostFragment
, so fragment manager cant find fragment by tag (?)
Please help me guys, I research this problem second day!
Upvotes: 4
Views: 4620
Reputation: 58
I encountered the same problem. As described here you have to make sure that the IDs of the bottom navigation menu items are identical to those in the navigation graphs. So in your case the bottom navigation menu should look similar to this:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/home_nav"
android:icon="..."
android:title="..." />
<item
android:id="@+id/search_nav"
android:icon="..."
android:title="..." />
<item
android:id="@+id/scheduled_nav"
android:icon="..."
android:title="..." />
<item
android:id="@+id/profile_nav"
android:icon="..."
android:title="..." />
</menu>
Upvotes: 1
Reputation: 4678
Today, I was facing the problem that I have a bottom navigation and respectively navigations. But fragments were not being changed on clicking bottom tabs. And then I solved the problem by giving same ID to navigation graph as I set in menu items. And this worked for me. And I got this clue with comment of @Ильмир Шагабиев as mentioned in the answer. Posting it as an answer because at firs time I did not notice this in comment. And after spending some time I came back to the same post and went through all comments.
Upvotes: 2
Reputation: 5257
You haven't set your FragmentContainerView
, you need to do as follow:
<androidx.fragment.app.FragmentContainerView
android:id="@+id/nav_host_container"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="@navigation/mobile_navigation" />
Have a look at this codelab, you can replace fragment
by FragmentcontainerView
and it'll work out.
In case the xml isn't fixing your issue can you post your nav_graph
?
Upvotes: 1