片段事务完成后,避免片段弹回堆栈

时间:2016-06-04 11:10:34

标签: android fragment

我有3个片段的成像:A,B,C 我的片段将在我的活动中替换为FrameLayout。 每个片段的每个事务都有输入和输出的自定义动画(滑入和滑出),我的片段将推回到堆栈。 在我的活动的onBackPress方法我弹出片段如果可用。 和我的问题:如果我从片段A到B并且这个动画开始并且我立即按下后退按钮我的应用程序崩溃了这个例外:

  

致命的例外:主要                                                                   处理:                                                                   java.lang.IllegalStateException:指定的子级已有父级。您必须首先在孩子的父母上调用removeView()。                                                                       在android.view.ViewGroup.addViewInner(ViewGroup.java:4405)                                                                       在android.view.ViewGroup.addView(ViewGroup.java:4246)                                                                       在android.view.ViewGroup.addView(ViewGroup.java:4186)                                                                       在android.view.ViewGroup.addView(ViewGroup.java:4159)                                                                       在android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1001)                                                                       在android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1165)                                                                       在android.app.BackStackRecord.popFromBackStack(BackStackRecord.java:1747)                                                                       在android.app.FragmentManagerImpl.popBackStackState(FragmentManager.java:1631)                                                                       在android.app.FragmentManagerImpl $ 2.run(FragmentManager.java:570)                                                                       在android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1572)                                                                       在android.app.FragmentManagerImpl $ 1.run(FragmentManager.java:483)                                                                       在android.os.Handler.handleCallback(Handler.java:751)                                                                       在android.os.Handler.dispatchMessage(Handler.java:95)                                                                       在android.os.Looper.loop(Looper.java:154)                                                                       在android.app.ActivityThread.main(ActivityThread.java:5969)                                                                       at java.lang.reflect.Method.invoke(Native Method)                                                                       在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:801)                                                                       在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:691)

我的片段类代码是:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    if (view == null) {
        view = inflater.inflate(R.layout.fragment_contact, container, false);
        initViews(view);
    }

    return view;
}
在事务运行之前,

是否可以避免使用popBackStack?还是更好的解决方案?

0 个答案:

没有答案