0

我决定使用他们自己的共享视图模型创建单独的功能。当只有一个 nav_graph 存在时,一切正常。但是在我将主图与主图和功能分开之后,所有的都崩溃了

我有 2 个导航图。

  1. 主要的 nav_graph
  2. 特征的第二张图

我正在尝试从第一张图导航到第二张图。没关系。应用程序工作正常。

但是当我尝试使用此代码获取共享 viewModel 时会出现问题。

    @IdRes navGraphId: Int,
    qualifier: Qualifier? = null,
    noinline parameters: ParametersDefinition? = null
): Lazy<VM>  = lazy{
    val store = findNavController().getViewModelStoreOwner(navGraphId).viewModelStore
    getKoin().getViewModel(ViewModelParameter(VM::class, qualifier, parameters, Bundle(), store, null))
}

我得到这个异常

 java.lang.IllegalArgumentException: No destination with ID 2131689472 is on the NavController's back stack. The current destination is Destination(com.bykhkalo.apps.walletapp:id/create_outgoing_fragment)

怎么了 ?

有我的导航图

主图

<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/nav_graph"
    app:startDestination="@id/summary_fragment">

    <include app:graph="@navigation/create_outgoing_graph"/>

    <fragment
        android:id="@+id/summary_fragment"
        android:name="com.bykhkalo.apps.walletapp.feature.summary.view.SummaryFragment"
        android:label="@string/summary_fragment_title"
        tools:layout="@layout/fragment_summary"
        >

        <action
            android:id="@+id/action_summaryFragment_to_choseEventTypeFragment"
            app:destination="@id/chose_event_type_fragment"
            app:popUpToInclusive="true"
            />

    </fragment>

    <fragment
        android:id="@+id/chose_event_type_fragment"
        android:name="com.bykhkalo.apps.walletapp.feature.summary.view.ChoseEventTypeFragment"
        tools:layout="@layout/fragment_chose_event_type"
        >

        <action
            android:id="@+id/action_choseEventTypeFragment_to_createOutgoingFragment"
            app:destination="@id/create_outgoing_graph"
            app:popUpToInclusive="true"
            />

    </fragment>

</navigation>

特征图

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    app:startDestination="@id/create_outgoing_fragment"
    android:id="@+id/create_outgoing_graph">


    <fragment
        android:id="@+id/create_outgoing_fragment"
        android:name="com.bykhkalo.apps.walletapp.feature.summary.view.CreateOutgoingFragment"
        tools:layout="@layout/fragment_create_outgoing">

        <action
            android:id="@+id/action_createOutgoingFragment_to_createCheckFragment"
            app:destination="@id/create_check__fragment"
            app:popUpToInclusive="true"
            />

        <action
            android:id="@+id/action_createOutgoingFragment_to_chooseOutgoingCategoryFragment"
            app:destination="@id/chose_outgoing_category_fragment"
            app:popUpToInclusive="true"
            />

    </fragment>

    <fragment
        android:id="@+id/create_check__fragment"
        android:name="com.bykhkalo.apps.walletapp.feature.summary.view.CreateCheckFragment"
        tools:layout="@layout/fragment_create_check"/>

    <fragment
        android:id="@+id/chose_outgoing_category_fragment"
        android:name="com.bykhkalo.apps.walletapp.feature.summary.view.ChooseOutgoingCategoryFragment"
        tools:layout="@layout/fragment_choose_outgoing_category"/>

</navigation>
4

0 回答 0