IllegalStateException while using fragmentManager.popBackStackImmediate();

Created a function to remove any fragments present before setting a new fragment to the container. The code is as below :

private static void removeAllFragments(FragmentManager fragmentManager) {
    while (fragmentManager.getBackStackEntryCount() > 0) {
        fragmentManager.popBackStackImmediate();
    }
}

The above code is called inside a function where the fragment is setup. The code is as below :

public void setupFragment(){
    fragmentManager = getFragmentManager();
    removeAllFragments(Objects.requireNonNull(fragmentManager));
    Log.d(TAG, "setupFragment: getBackStackEntryCount = " + 
    Objects.requireNonNull(fragmentManager).getBackStackEntryCount());
    fragmentTransaction = Objects.requireNonNull(fragmentManager).beginTransaction();
    ApprovalFragment approvalFragment = new ApprovalFragment();
    fragmentTransaction.add(R.id.rel2, approvalFragment);
    fragmentTransaction.addToBackStack("approval");
    fragmentTransaction.commit();
}

After opening many other apps and once this app is evicted from memory, a crash occurs when the app is opened. The error is as below :

java.lang.IllegalStateException: FragmentManager is already executing transactions
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
    at android.app.ActivityThread.-wrap12(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6077)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
 Caused by: java.lang.IllegalStateException: FragmentManager is already executing transactions
    at android.support.v4.app.FragmentManagerImpl.ensureExecReady(FragmentManager.java:2178)
    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2238)
    at android.support.v4.app.FragmentManagerImpl.popBackStackImmediate(FragmentManager.java:836)
    at android.support.v4.app.FragmentManagerImpl.popBackStackImmediate(FragmentManager.java:796)
    at com.example.fragment.HomeFragment.removeAllFragments(HomeFragment.java:650)
    at com.example.fragment.HomeFragment.setupFragment(HomeFragment.java:565)
    at com.example.fragment.HomeFragment.onCreateView(HomeFragment.java:420)
    at android.support.v4.app.Fragment.performCreateView(Fragment.java:2343)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1421)
    at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1752)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1821)
    at android.support.v4.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3251)
    at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:3205)
    at android.support.v4.app.FragmentController.dispatchActivityCreated(FragmentController.java:195)
    at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:597)
    at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:177)
    at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1249)
    at android.app.Activity.performStart(Activity.java:6722)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2609)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
    at android.app.ActivityThread.-wrap12(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:154) 
    at android.app.ActivityThread.main(ActivityThread.java:6077) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 

Answer

you have to use .getFragmentChildManager() instead of .getFragmentManager() with a android.support.v4.app.Fragment; else you’ll get such IllegalStateException thrown.

it is unclear, how/where HomeFragment.removeAllFragments() is being called… and I wonder why you not just inflate, which would replace the previous fragment, unless calling .addToBackStack(). that log is missing the output of Log.d, which makes it difficult to understand what the problem is.

using .popBackStack() instead of .popBackStackImmediate() might also work there.

based upon the situation, which you describe… my best guess would be to check for savedInstanceState == null and only run that code which causes the crash then.