3

I use Android Navigation Architecture Component.

I have three Fragment A, B and C

  • Fragment A -> Fragment B -> Fragment D
  • Fragment A -> Fragment C -> Fragment D

On fragment D I have a button used to validate modifications and finish. From this Frgament D I would like to:

  1. return to Fragment B when I come from Fragment B (I use findNavController().navigateUp())
  2. come back to Fragment A when I come from Fragment C (I don't know how to do it)

How can I go back directly to Fragment A when I come from Fragment D without going back to Fragment C?

Should I wait for the return on Fragment C and reprocess for the execution of findNavController().NavigateUp().

Dev Loots
  • 708
  • 9
  • 28
  • Check this, https://stackoverflow.com/questions/14971780/how-to-pop-fragment-off-backstack you can pop off the stack by name. – zgc7009 Apr 17 '20 at 13:42
  • thanks, but that forces me to create a condition in my Fragment D ? If previous fragment is C then go A, else If previous Fragment is B then go B? – Dev Loots Apr 17 '20 at 13:48
  • If you want to navigate like that, then why don't you simply use a fragment transaction ? – A_Singh7 Apr 17 '20 at 13:53
  • @A_Singh7 way better comment, I just glanced at the comment on my phone. For sure use a Fragment Transaction and manage your stack that way. – zgc7009 Apr 17 '20 at 14:03
  • 1
    I achieve this by using this code in fragment D : `if (isComeFromC == true) { findNavController().popBackStack(R.id.FragmentC, false) } findNavController().navigateUp() ` – Dev Loots Apr 17 '20 at 15:45
  • @Dev Loots he is trying to avoid that condition, at least based on his repose to my initial comment. – zgc7009 Apr 18 '20 at 15:43

3 Answers3

1

I use setPopUpTo on NavOptions, or you can use it inside the xml as well

documentation => https://developer.android.com/reference/androidx/navigation/NavOptions.Builder#setPopUpTo(int,%20boolean)

Saba
  • 1,208
  • 14
  • 19
1

You can achieve both of these action with findNavController().NavigateUp()

Given the following graph for example: graph

   <?xml version="1.0" encoding="utf-8"?>
   <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/test_nav_graph"
        app:startDestination="@id/a">

        <fragment
            android:id="@+id/a"
            android:name="fragment.A"
            tools:layout="@layout/a_fragment"
            android:label="A" >
            <action
                android:id="@+id/action_a_to_b"
                app:destination="@id/b" />
            <action
                android:id="@+id/action_a_to_c"
                app:destination="@id/c" />
        </fragment>
        <fragment
            android:id="@+id/b"
            android:name="fragment.B"
            tools:layout="@layout/b_fragment"
            android:label="B" >
            <action
                android:id="@+id/action_b_to_d"
                app:destination="@id/d" />
        </fragment>
        <fragment
            android:id="@+id/c"
            tools:layout="@layout/c_fragment"
            android:name="fragment.C"
            android:label="C" >
            <action
                android:id="@+id/action_c_to_d"
                app:destination="@id/d"
                app:popUpTo="@id/a"/>
        </fragment>
        <fragment
            android:id="@+id/d"
            tools:layout="@layout/d_fragment"
            android:name="fragment.D"
            android:label="D" />
    </navigation>

When you take the path: A->B->D your back stack is A,B,D and when you call NavigateUp() you go back to B.

Now the path A->C->D has a minor addition in the action tag as you can see

    <action
    android:id="@+id/action_c_to_d"
    app:destination="@id/d"
    app:popUpTo="@id/a"/>

it has popUpTo, so when you navigate from C to D app:popUpTo tells the Navigation library to pop some destinations off of the back stack as part of the call to navigate(), so after navigating to D your back stack is A,D and NavigateUp() takes you back to A

Ben Shmuel
  • 1,819
  • 1
  • 11
  • 20
1

So when you are in Fragment D and pressing back button . You can check the fragment stack and check for example

onBackPress(){
childFragmentManager.popbackstack()
val currentFragment = findFragmentByTag("FRAGMENT_B")
if(currentFragment == childFragmentManager.primaryNavigation()){
childFragmentManager.popbackstack()
}

NOTE

Above snippet is written without editor. You can use this logic.

sandeep dhami
  • 188
  • 1
  • 10