引起:java.lang.IllegalStateException:当从活动中替换任何片段时,无法在onSaveInstanceState之后执行此操作

时间:2017-08-31 05:33:38

标签: java android android-fragments

我正在尝试替换listeview setOnItemClickListener上的片段,但遇到崩溃问题:

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=65599, result=-1, data=Intent { (has extras) }} to activity {com.demo/com.demo.ui.SipHome}: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
at android.app.ActivityThread.deliverResults(ActivityThread.java:4323)
android.app.ActivityThread.handleSendResult(ActivityThread.java:4366)
at android.app.ActivityThread.-wrap19(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1649)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6540)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1527)
at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1545)
at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:654)
at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:621)
at com.demo.eu.siacs.conversations.ui.StartConversationActivity.openConversationFromConversationList(StartConversationActivity.java:3436)
at com.demo.eu.siacs.conversations.ui.StartConversationActivity.onActivityResult(StartConversationActivity.java:1743)
at android.support.v4.app.FragmentActivity.onActivityResult(FragmentActivity.java:165)
at paycalldailer.paycall.co.il.ui.SipHome.onActivityResult(SipHome.java:1877)
at android.app.Activity.dispatchActivityResult(Activity.java:7237)
at android.app.ActivityThread.deliverResults(ActivityThread.java:4319)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4366) 
at android.app.ActivityThread.-wrap19(Unknown Source:0) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1649) 
at android.os.Handler.dispatchMessage(Handler.java:105) 
at android.os.Looper.loop(Looper.java:164) 
at android.app.ActivityThread.main(ActivityThread.java:6540) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 

这里我打开一个活动,点击浮动按钮,下面是代码:

Intent intent = new Intent(getActivity(), PaycallContacts.class);
startActivityForResult(intent, REQUEST_CREATE_CHAT);

然后On,onActivityResult()我试图替换像:

这样的片段
if (requestCode == REQUEST_CREATE_CHAT) {

Fragment currentFragment = getChildFragmentManager().findFragmentById(R.id.frameChat);
        if (currentFragment instanceof ConversationFragment && getChildFragmentManager().getBackStackEntryCount() > 1) {
            return;
        }

if(!getActivity().isFinishing()) {
            mConversationFragment = new ConversationFragment();
            FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
            transaction.replace(R.id.frameChat, mConversationFragment, "conversation");
            transaction.addToBackStack(null);
            transaction.commitAllowingStateLoss();
            transaction.commit();
        }

        //switchToConversation(conversation);
        openConversation();

        getChildFragmentManager().addOnBackStackChangedListener(addOnBackStackChangedListener);

}

有没有人有想法如何解决崩溃问题?

2 个答案:

答案 0 :(得分:1)

使用transaction.commitAllowingStateLoss()可能有效

答案 1 :(得分:0)

谢谢@liubaoyua 您的答案解决了我的问题,但transaction.commitAllowingStateLoss()不能单独工作 首先,我重写onSaveInstanceState(Bundle outState)方法,并从那里删除super()方法调用,然后commiteAllowingStateLoss()工作。

@Override
public void onSaveInstanceState(Bundle outState) {
    //super.onSaveInstanceState(outState);
}