https://kotlinlang.org logo
#android
Title
# android
b

Bruno_

03/03/2020, 6:55 PM
I couldn't find anything in the docs, I'm familiar with the concept of back stack https://developer.android.com/guide/components/activities/tasks-and-back-stack (in context of AndroidX navigation) let's say that fragment A is my main one (startDestination) and I go to frag B and then to frag C, meaning the stack looks like this A -> B -> C and then if I navigate from C to A, will it look like A -> B -> C -> A or just
A
? There's no back button on the nav bar when I do so so I assume it's the latter but I just want to make sure that it won't cause any problems in the future
I've just checked on a simillar example: if I do A -> B -> C and then go to B, then C then B etc then it looks like this A -> B -> C -> B -> C -> B -> C
a

August Gruneisen

03/03/2020, 7:04 PM
If you want to have functionality such as A -> B -> C and then navigate back to B, leaving the backstack looking like A - > B is possible. You have to "pop" the items off the backstack as you navigate. See https://developer.android.com/guide/navigation/navigation-navigate#pop
b

Bruno_

03/03/2020, 7:05 PM
I know about pop, the thing is that I want to go back to my main fragment (A) after pressing a button, but at the same time I want to leave the back button behavious as-is
a

August Gruneisen

03/03/2020, 7:06 PM
If you navigate to fragment (A) without popping the backstack, the backstack will remain as it was and A will be added to the end
If you wish to also clear the backstack while navigating to A, you can use app:popUpTo
b

Bruno_

03/03/2020, 7:08 PM
okay so maybe I should give a real example of what I'm trying to achieve
here you can see a list of filters, there's also a navbar (which you can't see cause it's AS editor)
and here's the navigation
when you select a filter I want to go all the way back to the main screen
but when you click on a back button on the navbar then you simply go to the previous screen which is the default behaviour
wait it works I think
a

August Gruneisen

03/03/2020, 7:13 PM
So the question becomes - when you are back in the main screen, do you want the backstack to include your previous screens (selectFilterScreen, dashboard)? In this case, just navigate as normal, and the backstack will include these (meaning, when you click "back", they will show up in reverse order) Otherwise, if you want the new "recentActivities" to have a blank-slate (meaning navigating back from here will not bring you to selectFilterScreen) then you should include app:popUpTo in your <action> element. Hope this helps!
b

Bruno_

03/03/2020, 7:18 PM
popUpTo weirdly didn't seem to work, this one does tho
Copy code
public boolean popBackStack(@IdRes int destinationId,
                            boolean inclusive)
thanks @August Gruneisen ❤️
a

August Gruneisen

03/03/2020, 7:19 PM
Ahh yes, inclusive was the key 🙂