Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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()

like image 967
Vasili Fedotov Avatar asked Dec 23 '22 05:12

Vasili Fedotov


2 Answers

What I ended up doing was

Navigation.findNavController(view).popBackStack(R.id.fragmant_a,false)
like image 71
Vasili Fedotov Avatar answered Dec 31 '22 14:12

Vasili Fedotov


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.

Edit: The full form of your action under FragmentC tag of your navigation.xml file will be something similar to this:

<fragment
    android:id="@+id/FragmentC"
    android:name="com.yourdomain.FragmentC"
    android:label="FragmentC">
    <action
        android:id="@+id/action_fragmentC_to_fragmentA"
        app:destination="@id/fragmentA"
        app:popUpTo="@id/fragmentA"
        app:popUpToInclusive="false"/>
</fragment>
like image 22
Nik Mohammad Avatar answered Dec 31 '22 12:12

Nik Mohammad