Search code examples
androidandroid-jetpackandroid-architecture-navigation

Remove Fragments from Backstack using Navigation Component


I am sure there are many questions similar to this problem. However, I could not find a solution to the problem I am in. The problem is that popBackStack funtion removes the fragment from backstack. However, it crashes when another new navigation happens. Let me give an example, I have three fragments, FragmentFirst, FragmentSecond, and FragmentThird. One navigation graph,

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

    <fragment
        android:id="@+id/firstFragment"
        android:name="com.example.myapplication.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.myapplication.SecondFragment"
        android:label="SecondFragment" >
        <action
            android:id="@+id/action_secondFragment_to_thirdFragment"
            app:destination="@id/thirdFragment" />
    </fragment>
    <fragment
        android:id="@+id/thirdFragment"
        android:name="com.example.myapplication.ThirdFragment"
        android:label="ThirdFragment" >
    </fragment>
</navigation>

FirstFragment,

button.setOnClickListener {
    findNavController().navigate(R.id.action_firstFragment_to_secondFragment)
}

SecondFragment,

button2.setOnClickListener {
    view.findNavController().navigate(R.id.action_secondFragment_to_thirdFragment)
}

ThirdFragment,

button3.setOnClickListener {
    findNavController().popBackStack(R.id.firstFragment, true)
}

Now, I can navigate from First to Second, Second to Third, and Third to First. But When I press the button in First again, it crashes and the message is

id/action_firstFragment_to_secondFragment cannot be found from the current destination NavGraph

If I create an action in the graph like,

<fragment
    android:id="@+id/thirdFragment"
    android:name="com.example.myapplication.ThirdFragment"
    android:label="ThirdFragment" >
    <action
        app:popUpTo="@id/firstFragment"
        app:popUpToInclusive="true"
        android:id="@+id/action_thirdFragment_to_firstFragment"
        app:destination="@id/firstFragment" />
</fragment>

And in Fragment third,

button3.setOnClickListener {
    findNavController().navigate(R.id.action_thirdFragment_to_firstFragment)
}

In this case, it works perfectly. However, I want to use popBackStack due to the constraints of the app I am working in.

Thanks in Advance.


Solution

  • popBackStack(R.id.firstFragment, true) pops everything up to and inclusive to the first fragment. You've popped every fragment off the back stack. Seems like you want to use false, not true if you just want to return to the first fragment.