Android NAvigation with BottomNavigationView does not save UI state

Viewed 31

I have a Fragment with bottom tabs named TabsFragment

class BottomTabsFragment : Fragment(R.layout.fragment_tabs) {
    private val bottomNavigationView: BottomNavigationView by lazy {
        requireView().findViewById(R.id.bottom_navigation_view)
    }
    private val tabsNavigation: NavController by lazy {
        (childFragmentManager.findFragmentById(R.id.tabs_host_fragment) as NavHostFragment).navController
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        bottomNavigationView.setupWithNavController(tabsNavigation)
    }
}

Here is a menu for bottom navigation view:

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@id/dashboard_navigation"
        android:title="Dashboard" />

    <item
        android:id="@id/search_vehicle_navigation"
        android:title="Search" />

    <item
        android:id="@id/todos_navigation"
        android:title="Todos" />

    <item
        android:id="@id/activities_navigation"
        android:title="Activities" />

    <item
        android:id="@id/more_navigation"
        android:title="More" />
</menu>

Graph for bottom menu:

<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/tabs_navigation"
    app:startDestination="@id/search_vehicle_navigation">

    <include app:graph="@navigation/dashboard_navigation" />
    <include app:graph="@navigation/search_vehicle_navigation" />
    <include app:graph="@navigation/todos_navigation" />
    <include app:graph="@navigation/activities_navigation" />
    <include app:graph="@navigation/more_navigation" />

</navigation>

Everything is done according Google tutorials and samples and it does not work. Only 1 default fragment that is setted as StartDestination is saved (Becouse it is in stack to be displayed when user press back button):

https://developer.android.com/guide/navigation/navigation-multi-module

https://github.com/android/architecture-components-samples/tree/main/NavigationAdvancedSample

0 Answers
Related