'AndroidX navigation navigateUp twice

I am using the new AndroidX navigation framework.

I have a few fragments all linked in a navigation chain.

FragmentA --> FragmentB --> FragmentC

FragmentC has a Cancel button that should send me up all the way back to FragmentA.

Should I do the following:
on FragmentC call the method:

 Navigation.findNavController(view).navigateUp();

then on FragmentB listen to some callback and using some passed parameter or argument trigger another navigateUp() function from FragmentB

or is there some method that will do the equivalent of navigateUpTwice()



Solution 1:[1]

What I ended up doing was

Navigation.findNavController(view).popBackStack(R.id.fragmant_a,false)

Solution 2:[2]

You can set pop To FragmentA in your action from FragmentB --> FragmentC and when then you press back it goes to Fragment A instead of Fragment B

Solution 3:[3]

Try with this, it works for me.

findNavController().navigateUp()
findNavController().navigateUp()

Solution 4:[4]

In your navigation.xml file under the action that you have created to navigate to starting fragment (FragmentA in your case) add the following

<action
....
app:popUpTo="@id/fragmentA"
app:popUpToInclusive="false"/>

This will pop up all the fragments until FragmentA and will exclude FragmentA since popUpToInclusive is set to false.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Vasili Fedotov
Solution 2 shakil.k
Solution 3 vibroto
Solution 4 Nik Mohammad