androidkotlinnavigationfragment-backstacknavgraph

java.lang.IllegalArgumentException: Navigation action/destination


I'm building a notes application. I want to save the note when user press the back button from the createsNotesFragment and finish the createNotesFragment so that I can move to the homeFragment. The code I've used to do this is below:

activity?.onBackPressedDispatcher?.addCallback(this){
            saveNote()
            activity?.supportFragmentManager?.popBackStack()
        }

This is working fine. But when I try to again open the createNotesFragment to add an another note it shows me this error.

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.lavanianotesapp, PID: 4952
    java.lang.IllegalArgumentException: Navigation action/destination com.example.lavanianotesapp:id/action_homeFragment_to_createNotesFragment cannot be found from the current destination Destination(com.example.lavanianotesapp:id/createNotesFragment) label=fragment_create_notes class=com.example.lavanianotesapp.UI.Fragments.CreateNotesFragment
        at androidx.navigation.NavController.navigate(NavController.kt:1540)
        at androidx.navigation.NavController.navigate(NavController.kt:1472)
        at androidx.navigation.NavController.navigate(NavController.kt:1454)
        at androidx.navigation.NavController.navigate(NavController.kt:1437)

Here is my 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"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/nav_graph"
    app:startDestination="@id/homeFragment">

    <fragment
        android:id="@+id/homeFragment"
        android:name="com.example.lavanianotesapp.UI.Fragments.HomeFragment"
        android:label="fragment_home"
        tools:layout="@layout/fragment_home" >
        <action
            android:id="@+id/action_homeFragment_to_createNotesFragment"
            app:destination="@id/createNotesFragment" />
        <action
            android:id="@+id/action_homeFragment_to_editNotesFragment"
            app:destination="@id/editNotesFragment" />
    </fragment>
    <fragment
        android:id="@+id/createNotesFragment"
        android:name="com.example.lavanianotesapp.UI.Fragments.CreateNotesFragment"
        android:label="fragment_create_notes"
        tools:layout="@layout/fragment_create_notes" >
        <action
            android:id="@+id/action_createNotesFragment_to_homeFragment"
            app:destination="@id/homeFragment" />
        <action
            android:id="@+id/action_createNotesFragment_to_addLabelFragment"
            app:destination="@id/addLabelFragment" />
    </fragment>
    <fragment
        android:id="@+id/editNotesFragment"
        android:name="com.example.lavanianotesapp.UI.Fragments.EditNotesFragment"
        android:label="fragment_edit_notes"
        tools:layout="@layout/fragment_edit_notes" >
        <action
            android:id="@+id/action_editNotesFragment_to_homeFragment"
            app:destination="@id/homeFragment" />
        <action
            android:id="@+id/action_editNotesFragment_to_addLabelFragment"
            app:destination="@id/addLabelFragment" />
    </fragment>
    <fragment
        android:id="@+id/addLabelFragment"
        android:name="com.example.lavanianotesapp.UI.Fragments.AddLabelFragment"
        android:label="fragment_add_label"
        tools:layout="@layout/fragment_add_label" >
        <action
            android:id="@+id/action_addLabelFragment_to_createNotesFragment"
            app:destination="@id/createNotesFragment" />
        <action
            android:id="@+id/action_addLabelFragment_to_editNotesFragment"
            app:destination="@id/editNotesFragment" />
    </fragment>
</navigation>

I don't understant why after popBackStack() I'm not able to open the createNotesFragment again. Is there any alternative of popBackStack or what I'm missing?


Solution

  • you could try check current destination of your current screen like this:

    //id = nav graph fragment id
            if (findNavController().currentDestination?.id == id) {
                //do something here
                findNavController().popBackStack()
            }