1

I add fragments to back stack. For instance: A, B, C, A, D. Then from D I want to update the second A.

If I use supportFragmentManager.findFragmentByTag("A"), I have a reference to the first A, that is in a bottom of the back stack.

I tried to search through fragments collection, getBackStackEntryAt(i), but with no success.

CoolMind
  • 26,736
  • 15
  • 188
  • 224
  • Currently I use `parentFragment` and `childFragmentManager` to access previous fragment, see https://stackoverflow.com/questions/14804526/getparentfragment-returning-null. – CoolMind Dec 24 '19 at 14:06
  • Sorry, `findFragmentByTag("A")` often references to the last "A", but not always. If I add many fragments, it can link to not last "A". This behaviour is weird. – CoolMind Dec 24 '19 at 17:24

1 Answers1

0

Sorry, looking at get the latest fragment in backstack I understood that findFragmentByTag(tag) finds the last fragment with required tag. In my case after adding many fragments it sometimes found not last fragment with tag, but a previous to last (with the same tag). Maybe there is a bug in my code, I don't know, why is that.

First I tried to use callbacks, then tried to use parentFragment and childFragmentManager, see getParentFragment returning null. In fragment D I called parentFragment and accessed to methods of A and could update the last A fragment. This solution has two problems.

1) Child fragment (D) inherits a toolbar from parent (A) and adds own buttons. And after clicking Back button we return not to a parent, but even quit parent (A).

2) If we want to update not previous fragment (so that between A and D are also 3 fragments), it seems to be impossible.

So, I cancelled that solution and changed the application behaviour. I clear a backstack where it is possible. So that we cannot have two A fragments in backstack. I use supportFragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE). Yes, that's a workaround, but I don't know, how to deal with that bug.

CoolMind
  • 26,736
  • 15
  • 188
  • 224