通过删除子片段获取IllegalStateException

时间:2015-04-27 13:39:22

标签: android android-fragments 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$1.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)

1 个答案:

答案 0 :(得分:1)

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

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

所以,getParentFragment()是缺失的部分。