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!