android – Fragment still exists after remove?-ThrowExceptions

Exception or error:

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

How to solve:

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()

###

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.

###

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();
    }

###

instead of the

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

try

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

and use the

fragmentmanager.popBackStack();

###

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();
}

###

          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());
                }


 }
    }

###

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

###

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

Leave a Reply

Your email address will not be published. Required fields are marked *