24

1 Add two fragments to FragmentManager

FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft1 = fm.beginTransaction();
ft1.add(containerId, fragment1, "fragment1");
ft1.addToBackStack(null);
ft1.commitAllowingStateLoss();

FragmentTransaction ft2 = fm.beginTransaction();
ft2.add(containerId, fragment2, "fragment2");
ft2.addToBackStack(null);
ft2.commitAllowingStateLoss();

2 Change mobile setting Developer Options-> Don't Keep Activities-> ON

3 Remove all fragments after activity was recreated

FragmentTransaction ft3 = fm.beginTransaction();
for(Fragment f : fm.getFragments())  ft3.remove(f); 
ft3.commitAllowingStateLoss();

My question is why fm.findFragmentByTag("fragment1") is NOT null after remove?

minSdkVersion 17 targetSdkVersion 22 compileSdkVersion 22

Jingqi Xu
  • 289
  • 3
  • 6

8 Answers8

14

try this code for remove the fragment

FragmentManager manager = getActivity().getSupportFragmentManager();
FragmentTransaction trans = manager.beginTransaction();
trans.remove(myFrag);
trans.commit();
manager.popBackStack();

i think you have not add this method

manager.popBackStack();

or

getSupportFragmentManager().popBackStack()
Saif
  • 723
  • 6
  • 21
4

use popBackStackImmediate() instead of popBackStack() which insure fragment is removed from backstack immediately. transection's remove method does not remove fragment from backstack it just for transection.

CoolMind
  • 26,736
  • 15
  • 188
  • 224
CodingRat
  • 1,934
  • 3
  • 23
  • 43
1

Try this...

    List<Fragment> list = getSupportFragmentManager().getFragments();
    if (list == null) {
        Log.e("TAg", "No existing fragments" );
    }

    for (Fragment frag : list)
    {
        // To save any of the fragments, add this check.
        // A tag can be added as a third parameter to the fragment when you commit it
        if (frag.getTag().equals("tag-name")) {
            continue;
        }

        getSupportFragmentManager().beginTransaction().remove(frag).commit();
    }
Silambarasan Poonguti
  • 9,386
  • 4
  • 45
  • 38
1

I was having the same problem. I removed the fragment by using the tag as follows:

FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction ft = fragmentManager.beginTransaction();
Fragment fragment = fragmentManager.findFragmentByTag("fragment1");
ft.remove(fragment);
ft.commit();

Now findFragmentByTag() should return null

Saurabh
  • 426
  • 1
  • 7
  • 18
0

instead of the

ft1.add(containerId, fragment1, "fragment1");

try

ft1.replace(containerId, fragment1, "fragment1");

and use the

fragmentmanager.popBackStack();
Sandy
  • 985
  • 5
  • 13
0

you should commit individual fragment remove transactions.

this code will commit after loop complete

for(Fragment f : fm.getFragments())  ft3.remove(f); 
ft3.commitAllowingStateLoss();

instead of use brackets

for(Fragment f : fm.getFragments()){
 ft3.remove(f); 
 ft3.commitAllowingStateLoss();
}
Kishore Jethava
  • 6,666
  • 5
  • 35
  • 51
0
          public  void popAll() {
            if (mFragmentManager != null) {
                if (!mOnPause && mFragmentManager.getBackStackEntryCount() > 0) {
                    if (isFragmentOnStack(HomeFragment.TAG_NAME))
                        mFragmentManager.popBackStackImmediate(HomeFragment.TAG_NAME, 0);
                    else
                        mFragmentManager.popBackStackImmediate(null,
                                FragmentManager.POP_BACK_STACK_INCLUSIVE);
                } else if (mFragmentManager.getBackStackEntryCount() > 0) {
                    mTransactionList.clear();
                    mTransactionList.add(new FragmentPopAll());
                }


 }
    }
Srishti Roy
  • 576
  • 5
  • 17
0

Use commitNow method instead of commitAllowingStateLoss, as it perform transaction synchronously

Zain Ali
  • 15,535
  • 14
  • 95
  • 108