使用fragmentManager.popBackStackImmediate()时发生IllegalStateException。

时间:2018-10-10 05:38:44

标签: android fragment illegalstateexception

创建了一个函数,用于在将新片段设置到容器之前删除存在的任何片段。代码如下:

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

以上代码在设置片段的函数内部调用。代码如下:

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

打开许多其他应用程序后,一旦该应用程序从内存中退出,则在打开该应用程序时会发生崩溃。错误如下:

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) 

2 个答案:

答案 0 :(得分:0)

您必须使用.getFragmentChildManager()代替.getFragmentManager()android.support.v4.app.Fragment;否则您会被抛出IllegalStateException

目前尚不清楚HomeFragment.removeAllFragments()的调用方式/位置...而且我想知道为什么您不只是充气,它会替换前面的片段,除非调用.addToBackStack()。该日志缺少Log.d的输出,这使得很难理解问题所在。

使用.popBackStack()代替.popBackStackImmediate()可能也可以。

根据您所描述的情况...我最好的猜测是检查savedInstanceState == null,然后只运行导致崩溃的代码。

答案 1 :(得分:0)

发生这种类型的问题是因为从后堆栈中弹出片段之后,您会立即将新的片段添加到后堆栈中。

尽量不要将新的片段添加到堆栈中。