发生配置更改并因此而重新创建我的“ Activity ”和“片段”时,我的导航图作用域ViewModel不可用,而片段已被再次创建。
在navGraph之前,似乎重新创建了Fragment。
我正在使用以下代码从Fragment初始化navGraph范围内的ViewModel:

private val myViewModel: MyViewModel by navGraphViewModels(R.id.nav_graph_id)
如果尝试在Fragments myViewModel函数中使用onViewCreated,则在更改配置后会收到IllegalArgumentException。异常(exception):
java.lang.IllegalArgumentException: No destination with ID <destination id> is on the NavController's back stack
我该如何处理?
I have already checked that my ID isn't used anywhere else.
编辑1:
这是我的activity_main.xml
<?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"
        tools:context=".MainActivity">

    <androidx.fragment.app.FragmentContainerView
            android:id="@+id/main_nav_host"
            android:name="androidx.navigation.fragment.NavHostFragment"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:defaultNavHost="true"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:navGraph="@navigation/main_nav_graph" />

</androidx.constraintlayout.widget.ConstraintLayout>
这是我的main_nav_graph.xml:
<navigation
        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:id="@+id/main_nav_graph"
        app:startDestination="@id/main_nav_graph_1">

    <navigation
            android:id="@+id/main_nav_graph_1"
            android:label="@string/nav_graph_1_label"
            app:startDestination="@id/nav_graph_1_start_fragment">
        <!-- nav graph stuff -->
    </navigation>

    <navigation
            android:id="@+id/main_nav_graph_2"
            android:label="@string/nav_graph_2_label"
            app:startDestination="@id/nav_graph_2_start_fragment">

        <fragment
                android:id="@+id/nav_graph_2_start_fragment"
                android:label="@string/nav_graph_2_start_fragment_label"
                android:name="my.package.ui.NavGraph2StartFragment"
                tools:layout="@layout/fragment_nav_graph_2_start">
        </fragment>
        <!-- In here is where I get the problem -->
    </navigation>

</navigation>

最佳答案

我的问题如下:
更改配置后,NavGraph返回其起始目的地,但是无论如何最后加载的 Activity 片段都将被加载。这意味着实际启动的Fragment和navGraph的当前目的地不同步。
当我的Fragment然后尝试加载navGraph范围内的ViewModel时,它失败了,因为navGraph认为它在与实际不同的Fragment上。
为了解决我的问题,我必须使用 Activity saveInstanceState Bundle保存和恢复navController的状态。 (来源:https://stackoverflow.com/a/59987336)

override fun onSaveInstanceState(savedInstanceState: Bundle) {
    super.onSaveInstanceState(savedInstanceState)
    savedInstanceState.putBundle("nav_state", fragment.findNavController().saveState())
}

// restore in RestoreInstanceState
override fun onRestoreInstanceState(savedInstanceState: Bundle) {
    super.onRestoreInstanceState(savedInstanceState)
    fragment.findNavController().restoreState(savedInstanceState.getBundle("nav_state"))
}

// or restore in onCreate
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    if (savedInstanceState?.containsKey("nav_state") == true) {
        fragment.findNavController().restoreState(savedInstanceState.getBundle("nav_state"))
    }
}

08-18 17:58