通过删除子片段获取 IllegalStateException

Getting IllegalStateException with removing a child fragment

我正在使用子片段。片段 A 添加子片段 B:

B fragment = new B(); // B is a child
getChildFragmentManager().beginTransaction().add(fragment, null).commit();

然后 B 用以下内容删除自己:

// in the B's code:
getChildFragmentManager().beginTransaction().remove(this).commit();

但我遇到了异常:

java.lang.IllegalStateException: Recursive entry to executePendingTransactions
            at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1473)
            at android.support.v4.app.FragmentManagerImpl.dispatchDestroy(FragmentManager.java:1965)
            at android.support.v4.app.Fragment.performDestroy(Fragment.java:2006)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
            at android.support.v4.app.FragmentManagerImpl.removeFragment(FragmentManager.java:1235)
            at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:710)
            at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)
            at android.support.v4.app.FragmentManagerImpl.run(FragmentManager.java:458)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5254)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

在子片段中,我们需要使用来自父片段的 FragmentManager

getParentFragment().getChildFragmentManager().beginTransaction().remove(this).commit();

因此,getParentFragment() 是缺少的部分。