在开始Fragment事务时发生IllegalStateException

时间:2019-11-26 12:57:31

标签: android

从翻新获得成功或失败响应后,我将显示一个警报对话框。警报对话框类正在扩展DialogFragment。

在显示对话框之前,如果单击“主页”按钮,则我的应用程序崩溃。

我已经在下面进行搜索和尝试,但这对我没有帮助。

 @Override
    protected void onPostResume() {
        super.onPostResume();
        isTransactionSafe = true;
        if(isTransactionPending)
            showDialog(alertDialog);
    }


    @Override
    protected void onPause() {
        super.onPause();
        isTransactionSafe = true;
    }



    public void showDialog(UpdateAlertDialog updateAlertDialog) {


        // DialogFragment.show() will take care of adding the fragment
        // in a transaction.  We also want to remove any currently showing
        // dialog, so make our own transaction and take care of that here.
        alertDialog=updateAlertDialog;
        if (isTransactionSafe) {
            FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
            Fragment prev = getSupportFragmentManager().findFragmentByTag("dialog");
            if (prev != null) {
                ft.remove(prev);
            }
            ft.addToBackStack(null);
            // Create and show the dialog.
            updateAlertDialog.show(ft, "dialog");
            isTransactionPending=false;
        }
        else{
            isTransactionPending=false;
        }
    }
}

这是我的堆栈跟踪,

  Process: com.nexge.zalcon, PID: 17629
    java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
        at androidx.fragment.app.FragmentManagerImpl.checkStateLoss(FragmentManagerImpl.java:1536)
        at androidx.fragment.app.FragmentManagerImpl.enqueueAction(FragmentManagerImpl.java:1558)
        at androidx.fragment.app.BackStackRecord.commitInternal(BackStackRecord.java:317)
        at androidx.fragment.app.BackStackRecord.commit(BackStackRecord.java:282)
        at androidx.fragment.app.DialogFragment.show(DialogFragment.java:172)
        at com.nexge.zalcon.activity.DetailsRecyclerView.showDialog(DetailsRecyclerView.java:400)
        at com.nexge.zalcon.activity.DetailsRecyclerView$2.onResponse(DetailsRecyclerView.java:349)
        at retrofit2.DefaultCallAdapterFactory$ExecutorCallbackCall$1$1.run(DefaultCallAdapterFactory.java:83)
        at android.os.Handler.handleCallback(Handler.java:794)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:176)
        at android.app.ActivityThread.main(ActivityThread.java:6651)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:824)

有人帮我解决这个问题...

2 个答案:

答案 0 :(得分:1)

onPause中的分配有误,只需将isTransactionSafe设置为false:

@Override
protected void onPause() {
    super.onPause();
    isTransactionSafe = false;
}

答案 1 :(得分:0)

我认为您应该检查

if (getActivity() != null && isAdded())

在显示对话框之前