3

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.

Community
  • 1
  • 1
Gillis Haasnoot
  • 2,229
  • 1
  • 19
  • 23
  • Are you replacing the `Fragment` from the `Fragment` itself? If so use `getChildFragmentManager()` - just need to clarify. – Mark Feb 02 '17 at 13:47
  • No replacing it from the MainActivity. see https://github.com/holoduke/fragmenttest/blob/master/app/src/main/java/com/test/fragment/gillis/testfragment/MainActivity.java – Gillis Haasnoot Feb 02 '17 at 13:51
  • 1
    I confirm that when popping to a fragment deeper in the stack, all fragments in between have their onCreateView called in 25.1.1. This has broken my app which was functioning well until updated to 25.1.1. Downgrading to 25.1.0 solved my issue. However my replacement goes like A->B->C->D which is different than your example, so I may not have encountered the problem you mentioned in 25.1.0. – Murat Ögat Feb 15 '17 at 20:17

1 Answers1

4

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.

paramedic
  • 41
  • 3