getFragments method returns only 1 fragment when using replace transactions, used to return all
Asked Answered
G

1

6

As of SDK 26.0.0-beta1 and above, making use of the getFragments https://developer.android.com/reference/android/support/v4/app/FragmentManager.html#getFragments() method returns a list with only 1 fragment and the size of the list is also always 1 (so this excludes any possible null entries that might have been misleading me), when using replace method for fragment navigation.

Keep in mind I am using getSupportFragmentManager, not getFragmentManager

Prior to this SDK version all fragment transactions done with replace would be listed in the getFragments method. This means that if I replaced 10 fragments then the getFragments would return a list containing all of those 10 fragments.

However, from SDK 26.0.0-beta1 - specifically - and above (26.0.0-alpha1 and below does not have this problem) the method always returns a list of size 1 containing only the last fragment that was replaced.

To circumvent this problem I started using add and hiding whichever was the previously visible fragment, and so far this worked for what I wanted, which was to check which is the first fragment in the getFragments list whenever I needed as well as seeing if a certain instance of a fragment is already in that list.

Now a new problem arises when I try to use shared elements transitions, which only works with replace (as far as my google fu allowed me to find) meaning that if I want to use shared elements transitions I have to return to using replace fragments instead of add, but I will be back to the initial problem again.

So now I am stuck in this predicament and hoping anyone has a solution for this:

  • Is there any way to fix this?
  • Is the getFragments suppose to only return 1 fragment when we only use the replace method or is this behavior an undocumented bug that is yet to be fixed?
  • Is it possible to make shared element transitions between fragments without using replace?
Graphy answered 19/10, 2017 at 10:20 Comment(0)
S
5

I know this is an old post and you've probably solved it by now, but it's the first thing I found whilst Googling the same problem so hopefully this helps someone (and probably myself when I search for the same problem in a few months' time).

If you call fragmentManager.getBackStackEntryCount() you should get a count of all your fragments on the backstack. Assuming you assigned your fragments a tag when you added/replaced them and then added them to the backstack you can then get the tag of a fragment on the backstack and use fragmentManager.findFragmentByTag() to get its instance.

To set a tag during fragment add/replace:

transaction.replace(R.id.fragment_container, newFragment, fragmentName)
transaction.add(R.id.fragment_container, newFragment, fragmentName)

And to add it to the backstack:

transaction.addToBackStack(fragmentName);

And then to loop through your backstack to find a particular fragment:

final FragmentManager fragmentManager = getSupportFragmentManager();
for (int i = fragmentManager.getBackStackEntryCount() - 1; i >= 0; i--)
{
    Fragment fragment = fragmentManager.findFragmentByTag(fragmentManager.getBackStackEntryAt(i).getName());
    // ...
}

I'm iterating through the fragments in reverse, so the last fragment I added will be returned first.

Slick answered 18/5, 2018 at 12:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.