Saturday, 20 August 2022

Android jetpack navigation does not work after adding multiple back stack support [2.4.+]

I updated the navigation library from 2.3.5 to 2.4.2. After update I started getting this error: java.lang.IllegalArgumentException: No destination with ID XXX is on the NavController's back stack. The current destination is null. The error occurs after the following steps: FirstFragment -> SecondFragment [with another BottomNavigationView] -> ThirdFragment -> SecondFragment [crash]

I'll try to describe what I'm doing:

I have two fragments with BottomNavigationView.

  • First Fragment

    class MainBottomNavFragment : Fragment() {
    
        private lateinit var navController: NavController
    
        override fun onStart() {
            super.onStart()
            if (!::navController.isInitialized) {
                navController =
                    Navigation.findNavController(requireActivity(), R.id.mainBottomNavHostFragment)
            }
            binding.bottomNavigation.setupWithNavController(navController)
        }
    }

Layout


    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout 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">
    
        <fragment
            android:id="@+id/mainBottomNavHostFragment"
            android:name="androidx.navigation.fragment.NavHostFragment"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:defaultNavHost="false"
            app:layout_constraintBottom_toTopOf="@+id/bottomNavigation"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:navGraph="@navigation/main_menu_nav_graph" />
    
        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/bottomNavigation"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toBottomOf="parent"
            app:menu="@menu/main_menu" />
    
    </androidx.constraintlayout.widget.ConstraintLayout>

main_menu_nav_graph


    <?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_bottom_nav_graph"
        app:startDestination="@id/first">
    
        <fragment
            android:id="@+id/first"
            android:name=".FirstFragment"
            android:label="FirstFragment" />
    
        <fragment
            android:id="@+id/second"
            android:name=".SecondFragment"
            android:label="SecondFragment" />
    
        <fragment
            android:id="@+id/third"
            android:name=".ThirdFragment"
            android:label="ThirdFragment">
            <action
            ... />
        </fragment>
    
        <navigation
            android:id="@+id/fourth"
            app:startDestination="@id/fourthFragment">
    
            <fragment
                android:id="@+id/fourthFragment"
                android:name=".FourthFragment"
                android:label="FourthFragment" />
            <fragment
                android:id="@+id/fourthFragmentA"
                android:name=".FourthFragmentA"
                android:label="FourthFragmentA" />
            <fragment
                android:id="@+id/fourthFragmentB"
                android:name=".FourthFragmentB"
                android:label="FourthFragmentB" />
        </navigation>
    </navigation>

  • Second Fragment looks like this:

    class SecondFragment : Fragment() {
    
        private lateinit var navController: NavController
    
        override fun onStart() {
            super.onStart()
    
            if (!::navController.isInitialized) {
                navController = Navigation.findNavController(requireActivity(), R.id.secondHostFragment)
                val inflater = navController.navInflater
                navController.setGraph(inflater.inflate(R.navigation.second_nav_graph), arguments)
            }
            binding.secondBottomNavigation.setupWithNavController(navController)
        }
    }

Layout


    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout 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">
    
        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/secondBottomNavigation"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toTopOf="parent"
            app:menu="@menu/second_menu" />
    
        <fragment
            android:id="@+id/secondHostFragment"
            android:name="androidx.navigation.fragment.NavHostFragment"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/secondBottomNavigation" />
    
    </androidx.constraintlayout.widget.ConstraintLayout>

second_nav_graph


    <?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/second_nav_graph"
        app:startDestination="@id/secondA">
    
        <fragment
            android:id="@+id/secondA"
            android:name=".SecondA"
            android:label="SecondA">
            <action
            ... />
        </fragment>
        <fragment
            android:id="@+id/secondB"
            android:name=".SecondB"
            android:label="SecondB" />
    </navigation>

Can I have two nested BottomNavigationView? In version 2.3.5 everything worked



from Android jetpack navigation does not work after adding multiple back stack support [2.4.+]

No comments:

Post a Comment