Search code examples
androidandroid-architecture-navigation

Avoid to popback to specific fragment with Navigation Architecture Component


I have two navigation flow with Navigation Architecture Component :

A > B > C

And

A > X > C

In the first flow, popBackStack() works as expected. But I want a different behavior in the second flow. I dont want to popback to Fragment X but directly to Fragment A.

Because Fragment C is accessible by several ways, I can't use a fixed destinationId in popBackStack(destinationId,inclusive).

Any solution ? May be navigation direction config ?


Solution

  • Use this action for x to c.

    <fragment
    android:id="@+id/x"
    ...
    >
     <action
                android:id="@+id/action_X_to_C"
                app:destination="@id/C"
                app:popUpTo="@id/A" >
            </action>
    </fragment>
    

    In fragmentX

    findNavController().navigate(R.id.action_x_to_C)