I'm creating a simple ToDo app and checking about the Navigation methods to return to my mainFragment from the AddTask Fragment. And I found that I can return using navigateUp() and also popBackStack(), but I don't understand the difference.
When I use this:
Navigation.findNavController(it).navigateUp()
Or this:
Navigation.findNavController(it).popBackStack()
I return to the mainFragment and I think, the addTaskFragment is popped from the stack, so could someone please explain me?
navigateUp() Attempts to navigate up in the navigation hierarchy. @MainThread boolean. popBackStack() Attempts to pop the controller's back stack.
popBackStack(backStackId, INCLUSIVE) will pop all the fragments (states) back to backStackId, so once you pop the lowest i that you're going to pop, all the higher ones should get popped at the same time.
app:popUpToInclusive="true"/> </fragment> </navigation> When the navigation graph is inflated, these actions are parsed, and corresponding NavAction objects are generated with the configurations defined in the graph. For example, action_b_to_a is defined as navigating from destination b to destination a .
The Up button is used to navigate within an app based on the hierarchical relationships between screens. For instance, if screen A displays a list of items, and selecting an item leads to screen B (which presents that item in more detail), then screen B should offer an Up button that returns to screen A.
Sorry, the (edit: previously) accepted answer is absolutely wrong, navigateUp()
will also pop the top fragment from the backstack, if there is any.
For starters: If you arrived at your current destination from within your app, they act exactly identical.
ONLY if you arrived at the current destination with a deeplink from a different app, they will behave differently:
navigateUp()
will leave your app and return to the app that navigated to the deep link in your app.
popBackStack()
will attempt to go back one step in your backstack, and will not do anything if there is no backstack entry.
You can read up on the differences on: https://developer.android.com/reference/androidx/navigation/NavController#navigateUp()
navigateUp() Attempts to navigate up in the navigation hierarchy. Suitable for when the user presses the "Up" button marked with a left (or start)-facing arrow in the upper left (or starting) corner of the app UI.
The intended behavior of Up differs from Back when the user did not reach the current destination from the application's own task. e.g. if the user is viewing a document or link in the current app in an activity hosted on another app's task where the user clicked the link. In this case the current activity (determined by the context used to create this NavController) will be finished and the user will be taken to an appropriate destination in this app on its own task.
and
popBackStack() Attempts to pop the controller's back stack. Analogous to when the user presses the system Back button when the associated navigation host has focus.
It's about android backstack. Whenever you launch an application the activity will be added to activities backstack(Activity1
e.g.). Now if you start Activity2
, it'll be added to top of the stack. If you press the back button, the Activity2
will be destroyed and popped from the top of the stack and the activity which had placed below Activity2
will be resumed. Check this figure to understand things better.
The stack is working for fragments too. Assuming you launch FragmentA
, then FragmentB
. So the stack will be like FragmentB
on top and FragmentA
below that. FragmentB
is showing on the screen now. If you use popBackStack()
obviously it'll destroy the FragmentB
and pop it from top of the stack, then it'll check for the current top fragment in the stack, that is FragmentA
, so it'll be resumed. But if you call navigateUp()
, it will navigate up through the stack, just like as it's name. When you navigate up from FragmentB
, you will reach FragmentA
in the stack again. This is why these two methods have the same output. But the point is that navigateUp()
will not pop FragmentB
from top of the stack and it'll not be destroyed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With