9

I am trying to find out what the previous fragment was in the backstack and if it is the one that I want, then go back to that otherwise go to a different fragment. I am having problems finding out the name/id of my previous fragment. My current setup is as follows:

Fragment 1 -> Fragment 2 -> Fragment 3 if the previous id of Fragment 3 is >Fragment 2 then go back to it with arguments.

But I also will have a situation where this will happen:

Fragment 4 -> Fragment 3 here I want to be able to also check if the previous id/name of Fragment 3 is equal to Fragment 4 and if it is go back to that Fragment with arguments.

Basically Fragment 3 will have different routes out of it and I want to be able to determine which previous fragment it will go to next.

My problem is I am not able to get access to information from previous Fragments. To check if any of this is possible. I am new to using the Android Navigation Component so any help is appreciated.

If the question seems a little bit confusing please let me know so I can rewrite it if needed.

Thanks!

Zain
  • 37,492
  • 7
  • 60
  • 84
OPWagg
  • 153
  • 1
  • 4
  • 1
    That's not how you should be returning results to a previous fragment, see [the comments on the existing feature request](https://issuetracker.google.com/issues/79672220#comment7). – ianhanniballake May 30 '19 at 17:22

2 Answers2

1

You'd use previousBackStackEntry which returns a NavBackStackEntry that is:

the previous visible entry on the back stack or null if the back stack has less than two visible entries

And to get the id of the previous destination use: previousBackStackEntry.destination.id but as it is nullable then use previousBackStackEntry?.destination?.id

Applying that in your scenario:

In Fragment 3:

val previousFragment = findNavController().previousBackStackEntry?.destination?.id

previousFragment?.let {
    when (previousFragment) {
        R.id.fragment_2_id ->
            // The previous fragment is Fragment 2

        R.id.fragment_4_id ->
            // The previous fragment is Fragment 4

        else ->
            // The previous fragment is neither Fragment 2 nor 4
    }
}
Zain
  • 37,492
  • 7
  • 60
  • 84
  • This will only ever return ids that didn't use a pop action. If the previous fragment uses an action that pops the stack, which is normal for returning from detail views, this id will instead be the "upper" (less detail level in drill down) id instead of the previous id. The method is not named correctly. – Henrik Erlandsson Jan 24 '23 at 16:49
0

An approach to solving this problem would be to pass a key as an argument from the previous fragment so that in the current fragment, you'll check what key was passed and act based on it. For example. Fragment A and B both navigate to Fragment C, and you want to act in fragment C based on the previous scenario. What you can do is make Fragment C take an argument "fragmentOrigin", then set the argument to "fragmentA" or "fragmentB" depending on the preceeding fragment. This way, in fragment C you can then check if fragmentOrigin == fragment A; {Do something} else if fragmentOrigin == fragment B {Do something else}