A
A
androidDev1232021-11-07 13:03:13
Android
androidDev123, 2021-11-07 13:03:13

How to fix application crash when trying to inject viewModel with sharedGraphViewModel(featureGraphId)?

Used in the sharedViewModel project.
Everything was fine when there was only one nav_graph.
But after I split the main graph into the main nav_graph and feature_graph, everything collapsed.

I have 2 navigation graphs.
1.Main nav_graph.
2. The second graph for the feature.
I'm trying to move from the first graph to the second. The application is working fine.

But the problem arises when I try to get a sharedViewModel using this code when starting a fragment with a feature graph.

inline fun <reified VM : ViewModel> Fragment.sharedGraphViewModel(
    @IdRes navGraphId: Int = R.id.nav_graph,
    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))
}


I get this Exception
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)


I can't figure out what could be the problem.
There is only one activity and only one FragmentContainerView

in the application Below is the navigation graph code

Main nav_graph.
<?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>


Feature 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"
    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>

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question