小编Mil*_*ček的帖子

添加多个后退堆栈支持后 Android jetpack 导航不起作用 [2.4.+]

我将导航库从2.3.5更新到2.4.2。更新后我开始收到此错误:java.lang.IllegalArgumentException: No destination with ID XXX is on the NavController's back stack. The current destination is null。执行以下步骤后会发生错误:FirstFragment -> SecondFragment [with another BottomNavigationView] -> ThirdFragment -> SecondFragment [crash]

我将尝试描述我正在做的事情:

我有两个片段BottomNavigationView

  • 第一个片段

    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)
        }
    }

Run Code Online (Sandbox Code Playgroud)

布局


    <?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" …
Run Code Online (Sandbox Code Playgroud)

android bottomnavigationview android-jetpack-navigation

6
推荐指数
0
解决办法
713
查看次数