-1

I am making A Trivia app following Udacity Android with Kotlin guide.

In Navigation Graph there are three destination screen; TitleFragment, GameFragment,GameOverFragment and GameWonFragment. (Images for code are included)

While constructing Navigation graph, we are setting up for system back key by setting popUpTo behavior attribute.

The guide says us to setup popUpTo (inclusive) attribute for action connecting GameFragment to GameOverFragment and GameWonFragment which will lead back to TitleFragment by popping up fragments including GameFragment on system back button hit. I understood till here.

But again the guide tells us to set action tag and popUpTo attribute from GameOverFragment and GameWonFragment to GameFragment. This time we wet popUpTo attribute(exclusive) for popping upto TitleFragment on system back key hit.

Both set up are doing same thing; taking us to TitleFragment on System back key hit. So, why setting up 2 times is necessary?

Akr
  • 117
  • 2
  • 10
  • Welcome to SO, make sure you add some code with a specific question next time so you can get an answer. – MadMax Feb 24 '21 at 16:15

1 Answers1

0

As the Documentation say :

When navigating using an action, you can optionally pop additional destinations off of the back stack. For example, if your app has an initial login flow, once a user has logged in, you should pop all of the login-related destinations off of the back stack so that the Back button doesn't take users back into the login flow.

To pop destinations when navigating from one destination to another, add an app:popUpTo attribute to the associated element. app:popUpTo tells the Navigation library to pop some destinations off of the back stack as part of the call to navigate(). The attribute value is the ID of the most recent destination that should remain on the stack.

You can also include app:popUpToInclusive="true" to indicate that the destination specified in app:popUpTo should also be removed from the back stack.

When using popUpTo , all destinations from current destination to the destination with the id in popUpTo will be deleted , except that destination with id which will remain in the back stack . And when using popUpToInclusive = true , then all destination and also the destination with the id in popUpTo will be deleted from the back stack !

This is more clear explain in detail about this , in the official documentation .

Enjoy !

Mustafa Ibrahim
  • 557
  • 3
  • 8