2

Suppose we have 3 fragments with toolbars with up button functionality

SplashFragment -> FragmentA -> FragmentB 

This is the expected flow of app. On reaching FragmentB when user presses back button he should go to FragmentA and again on pressing back button the app should exit. How can we achieve this behaviour using navigation component? I have tried to do this using the app:popUpTo inside the action tag, somehow I managed to make it work for hardware back button but the behaviour for up button didn't change.

Need help on this.

Ankit Bisht
  • 1,209
  • 12
  • 14

4 Answers4

2

Warning: this is deprecated on Navigation alpha08,And the link is the current solution.

This may be the answer you want. Use app:clearTask="true"

<fragment
    android:id="@+id/splashFragment"
    android:name="xxx.SplashFragment"
    android:label="fragment_splash"
    tools:layout="@layout/fragment_splash">
    <action
        android:id="@+id/action_splashFragment_to_mainFragment"
        app:destination="@id/mainFragment"
        app:enterAnim="@anim/anim_right_in"
        app:exitAnim="@anim/anim_left_out"
        app:popEnterAnim="@anim/anim_left_in"
        app:popExitAnim="@anim/anim_right_out"
        app:clearTask="true"/>

Navigation Architecture Component - Splash screen

peerless2012
  • 179
  • 1
  • 7
1

Here is my solution. fragmentA->fragmentB->fragmentc on back, button pressed from FragmentC to FragmentB and then again back pressed and you want to exit the form app. add this line to FragmentA action.

app:launchSingleTop="true"
app:popUpTo="@+id/fragmentA"
app:popUpToInclusive="true"
Anwar Zahid
  • 227
  • 3
  • 10
0

You may try to do it this way:

After App launches Fragment-A clear the backstack i.e.; SplashFragment , Now when the user navigates forward everything is same but after returning to Fragment-A , if he hits back button The app exits .

Hope this helps!

Dilip Krishna
  • 139
  • 3
  • 16
  • No thats not gonna work. Here we are talking about navigation component not a normal fragment transaction, I need to update the control flow of navigation but don't know how. – Ankit Bisht Oct 02 '18 at 05:43
  • Yeah, I understood that ,you need to clear your backstack programmatically – Dilip Krishna Oct 03 '18 at 15:28
0

I have a similar flow in place, what works and I believe is a clean solution is to have your FragmentA as the start point of your navigation graph (that will always put in top of the backstack on lunch) then you want to navigate (maybe conditionally) to the SplashFragment on the onStart (or earlier in the lifecycle) of your FragmentA, and finally when you would like your SplashFragment to go away, you usefindNavController().popBackStack() to pop the fragment. then only Fragment A and B will remain on your stack and back will work as expected.

hope this helps.

Kayvan N
  • 8,108
  • 6
  • 29
  • 38