-->

fragment popbackstack behaviour broken in 25.1.0 a

2020-07-18 20:17发布

问题:

Since support version 25.1.0 and the most recent 25.1.1 I got strange behaviour with fragment replacing/adding. There have been issues reported for 25.1.0 Android - fragmentTransaction.replace() not works on support library 25.1.0

But now in 25.1.1 i got similar issues. To reproduce the behaviour i created sample app which you can find at https://github.com/holoduke/fragmenttest

It is basically one Activity with a fragment container. A couple of fragments are available which will be dynamically replace each other by pressing a button. We start with adding FragmentA from the mainActivity itself.

    FragmentManager fm = getSupportFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();

    Fragment f = new FragmentA();
    fm.popBackStackImmediate(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
    f.setRetainInstance(false);
    ft.replace(R.id.fragmenttarget, f);
    ft.addToBackStack(null);
    ft.commit();

All good Works fine. in both 25.0.1, 25.1.0 and 25.1.1

Now in fragmentA there are 3 buttons which will all replace the current fragment with either fragmentA, fragmentB or fragmentC

the code for adding fragment B and C is almost the same as fragment A except we have not defined:

fm.popBackStackImmediate(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);

when fragment B or C is added the following code is executed:

    FragmentManager fm = getSupportFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();

    Fragment f = new FragmentB();
    f.setRetainInstance(false);
    ft.replace(R.id.fragmenttarget, f);
    ft.addToBackStack(null);
    ft.commit();

Still all good in both 25.0.1, 25.1.0 and 25.1.1. If you add fragmentB and C a couple of times the fm.getBackStackEntryCount() is increasing. Thats good.

Now the weird part. We want to add FragmentA with popStackImmediate (to clear history) Here the behaviour of both 3 support versions are going mad.

Lets say that you execute the following bavhiour in all 3 versions:

  1. start app
  2. replace with fragment B
  3. replace with fragment C
  4. replace with fragment B
  5. replace with fragment C
  6. replace with fragment A

in 25.0.1 everything works out fine. the backstack is cleared and onCreateView and ActivityCreated is called in FragmentA.

in 25.1.0 somehow after replacing with FragmentA the onCreateView and ActivityCreated are called 2 times. Not good.

in 25.1.1 its even worse. after replacing with fragmentA, for all views in the backstack the onCreateView and ActivityCreated are called. Now Thats funny right :)

Just try out my sample app and look in the logcat. change the support version in app.gradle file to see the differences.

I would be glad if someone is able recognise this issue as well, so we can find a way to overcome or even solve this issue.

回答1:

Well, I faced with the same problem and found a solution by comparing 25.0.1 -> 25.1.1 FragmentManager.class. Try to use setAllowOptimization method of FragmentTransaction.