1

I work on an app and this app has a list fragment as the starting point when you open the app. Now when I click on a listItem an update fragment opens and when you press the save button you go back to the list fragment. The problem is that when I now press the back button on my phone in order to exit the app that doesn't happen and instead I go back to the update fragment.

I would like to make it so that the update fragment doesn't get saved onto the backstack or at least I think that might do what I want. But after some research I still have a really hard time understanding the backstack and how to actually implement stuff around the backstack codewise so I hope that someone might be able to help me out. pls!

Edit1:

I added this line of code to my update button functionality.

fragmentManager?.popBackStack()

Now it looks like this:

1.) listFragment

2.) click on it -> open updateFragment -> press update button -> back to listFragment

3.) press back button of my phone and this happens so the update fragment doesn't open now but the text in the actionbar still appears as well as the back button.

Edit2:

I got rid of the line fragmentManager?.popBackStack() and changed the Pop Behavior of the NavigationAction that leads from my update fragment to my list fragment (for clarification: my list fragment is named nav_home).

nikenzo
  • 13
  • 3
  • You'd better to post some code for SO can help you out with your question. – Jorge Luiz Jun 19 '22 at 18:13
  • When you save you're probably adding the list fragment to the backstack - so when you start with `list > update` and you save, when you "go back" you're actually adding a new destination, so your backstack is `list > update > list`. And when you hit the back button to pop off the current fragment (the last `list` one) you end up back at `list > update`. So when you save, you probably want to call `popBackStack()` to go from `list > update` to `list`. (I'm just guessing because I have no idea how you've implemented it) – cactustictacs Jun 19 '22 at 18:15
  • Makes sense and I'd like to but I am not really sure what part of my code might be usefull regarding the question. I currently haven't implemented any code that specifically targets/manipulates backstack. – nikenzo Jun 19 '22 at 18:19
  • @cactustictacs what u wrote sounds like what i need yea. Is it enough to just write popBackStack() to get rid of the update fragment in the backstack? – nikenzo Jun 19 '22 at 18:22
  • Depends, are you doing a fragment transaction with `addToBackStack()` when you navigate to the *update* one, like they describe here? https://developer.android.com/guide/fragments/fragmentmanager Then popping that transaction off the stack will get you back where you were. If you're using the `Navigation` library it should work too, but it depends how you have that set up and if your navigation actions are intentionally popping things off the backstack. Try it and see? – cactustictacs Jun 19 '22 at 19:04
  • pls take a look at the question again, specifically Edit2. It works now but i don't quite understand what happens here. – nikenzo Jun 19 '22 at 19:44
  • @nikenzo so yeah if you are using the *Navigation* library then you should be doing stuff through that, e.g. `findNavController().popBackStack()` rather than messing with a `FragmentManager`. The `popUpTo` behaviour's explained here: https://developer.android.com/guide/navigation/navigation-navigate#pop but basically, when you define a route from *update* to *list*, normally that would be an extra step forward, another thing added onto the backstack that you'd have to backtrack through. You'd end up with `list > update > list`, and if you hit back you'd end up at `update` again – cactustictacs Jun 21 '22 at 18:36
  • But using `popUpTo` means instead of taking another step forward and adding to the backstack, it pops stuff off until it reaches the destination deeper in the stack. It backs through your fragment history, basically, so you go from `list > update` to `list`. Like hitting the back button on your browser to go back to the main page, instead of clicking a link that takes you there. `popUpToInclusive` would also pop `list` off the stack (i.e. "go back to whatever we were on before we navigated to the list") but if it's your *start destination* in the navigation graph you can't pop it off – cactustictacs Jun 21 '22 at 18:40
  • Ah okay okay I think I kinda get it, anyway thx a lot for the detailed explanation :D – nikenzo Jun 21 '22 at 19:54

1 Answers1

0

Author here:

I changed the Pop Behavior of the NavigationAction that leads from my update fragment to my list fragment (for clarification: my list fragment is named nav_home) and now it works but I can't really explain why exactly.

nikenzo
  • 13
  • 3