1

Newbie on Navigation Component. I'm trying to achieve this but I don't know how:

I have 3 fragments:

FragmentMainFragmentListFragmentAdd

If I try to go from FragmentMain to FragmentList but list is empty it should go directly to FragmentAdd (its not really directly since I'm checking the list on FragmentList but for the user it should look like it). If I'm on FragmentAdd and I go back (by either pressing back button or back on the toolbar) it should go back to FragmentMain (since FragmentList is still empty) but if I add something it should go back to FragmentList.

I've tried with

<fragment
    android:id="@+id/fragmentMain" >
    <action android:id="@+id/action_fragmentMain_to_fragmentList
        app:destination="@id/fragmentList"
        app:popUpTo="@id/fragmentMain"
        app:popUpToInclusive="true"/>
</fragment>

The thing is that if I press back on FragmentAdd it will go to FragmentList, check that the list is still empty and go back to FragmentAdd. And if I pop FragmentList and then add something It will go to FragmentMain and not the list.

The difference with Conditional back navigation with Android Navigation Component is that I can add stuff to FragmentList from FragmentAdd

Any suggestion please?

homerman
  • 3,369
  • 1
  • 16
  • 32

1 Answers1

0

Use app:popUpTo="@id/FragmentList" and app:popUpToInclusive="true" in action action_fragmentList_to_fragmentAdd

<fragment
    android:id="@+id/fragmentMain" >
    <action android:id="@+id/action_fragmentMain_to_fragmentList
        app:destination="@id/fragmentList"/>
</fragment>
<fragment
    android:id="@+id/fragmentList" >
    <action android:id="@+id/action_fragmentList_to_fragmentAdd
        app:destination="@id/fragmentAdd"
        app:popUpTo="@id/fragmentList"
        app:popUpToInclusive="true"/>
</fragment>
<fragment
    android:id="@+id/fragmentAdd" >
</fragment>
tktschool
  • 116
  • 1
  • 3