Navigation action/destination cannot be found from the current destination

Viewed 1444

In my app I have fragment_1, fragment_2 and activity_2. So my main navigation has this 2 fragments. From fragment 1 I can navigate to fragment 2 and from fragment 2 I can navigate to the activity 2. And when I'm navigating from fragment 2 to the aitivity 2, I have to pop this fragmet.

Here's my navigation file code.

<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_navigation"
app:startDestination="@id/firstFragment">

<fragment
    android:id="@+id/firstFragment"
    android:name="com.example.navcomponentbug.FirstFragment"
    android:label="fragment_first"
    tools:layout="@layout/fragment_first">

    <action
        android:id="@+id/action_firstFragment_to_secondFragment"
        app:destination="@id/secondFragment" />

</fragment>

<fragment
    android:id="@+id/secondFragment"
    android:name="com.example.navcomponentbug.SecondFragment"
    android:label="fragment_second"
    tools:layout="@layout/fragment_second">

    <action
        android:id="@+id/action_secondFragment_to_secondActivity"
        app:destination="@id/secondActivity"
        app:popUpTo="@id/firstFragment"
        app:popUpToInclusive="true"/>

</fragment>

<activity
    android:id="@+id/secondActivity"
    android:name="com.example.navcomponentbug.SecondActivity"
    android:label="activity_second"
    tools:layout="@layout/activity_second" />

</navigation>

Simple, right? So here is the navigation in first fragment:

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    val btn: Button = view.findViewById(R.id.btn_navigate_to_second_fragment)
    btn.setOnClickListener {
        findNavController().navigate(R.id.action_firstFragment_to_secondFragment)
    }
}

Then the same code is for the second fragment, which navigates to the activity, only the action is different (Don't want to paste the same code).

And the code for my activity 2:

binding.btnNavigateToFirstFragment.setOnClickListener {
        finish()
}

Seems everything is fine, but no. When I navigate back from my activity 2 to the first fragment, and click again on button to navigate to second fragment, I'm getting this error

java.lang.IllegalArgumentException: Navigation action/destination com.example.navcomponentbug:id/action_firstFragment_to_secondFragment cannot be found from the current destination NavGraph(com.example.navcomponentbug:id/main_navigation) startDestination={Destination(com.example.navcomponentbug:id/firstFragment) label=fragment_first class=com.example.navcomponentbug.FirstFragment}

So why is this happening? Thank you.

2 Answers

I also had the exact same exception in my project (but the reason is not necessarily the same). It happened to me because (without knowing it) I quickly clicked twice on the button (and the fault of the exception was precisely the second click). to solve the problem i put a boolean variable like this:

var isNavigating = false

and then, when I have to navigate, I check it

    btn.setOnClickListener {
   if (!isNavigating) {
            isNavigating = true
            findNavController().navigate(R.id.action_firstFragment_to_secondFragment)
        }
    }

This is to avoid multiple clicks made almost simultaneously

The issue was with the popBackStackInclusive="true". As said in the docs:

You can also include app:popUpToInclusive="true" to indicate that the destination specified in app:popUpTo should also be removed from the back stack.

So my specified destination was the first fragment and when I popped my my second fragment, first one was also popped.

So I've fixed this simply by removing app:popUpToInclusive="true"

The final code is this:

<action
    android:id="@+id/action_secondFragment_to_secondActivity"
    app:destination="@id/secondActivity"
    app:popUpTo="@id/firstFragment"/>
Related