在显示对话框时,我得到“在 onSaveInstanceState 之后无法执行此操作";

时间:2023-03-21
本文介绍了在显示对话框时,我得到“在 onSaveInstanceState 之后无法执行此操作";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

有些用户在举报,如果他们使用通知栏中的快速操作,他们会被强制关闭.

Some users are reporting, if they use the quick action in the notification bar, they are getting a force close.

我在调用 "TestDialog" 类的通知中显示了一个快速操作.在 TestDialog 类中按下贪睡"按钮后,我将显示贪睡对话框.

I show a quick action in the notification who calls the "TestDialog" class. In the TestDialog class after pressing the button "snooze", I will show the SnoozeDialog.

private View.OnClickListener btnSnoozeOnClick() {
    return new View.OnClickListener() {

        public void onClick(View v) {
            showSnoozeDialog();
        }
    };
}

private void showSnoozeDialog() {
    FragmentManager fm = getSupportFragmentManager();
    SnoozeDialog snoozeDialog = new SnoozeDialog();
    snoozeDialog.show(fm, "snooze_dialog");
}

错误是*IllegalStateException: Can not perform this action after onSaveInstanceState*.

触发 IllegarStateException 的代码行是:

snoozeDialog.show(fm, "snooze_dialog");

该类正在扩展FragmentActivity",而SnoozeDialog"类正在扩展DialogFragment".

The class is extending "FragmentActivity" and the "SnoozeDialog" class is extending "DialogFragment".

这是错误的完整堆栈跟踪:

java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1327)
at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1338)
at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:595)
at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:574)
at android.support.v4.app.DialogFragment.show(DialogFragment.java:127)
at com.test.testing.TestDialog.f(TestDialog.java:538)
at com.test.testing.TestDialog.e(TestDialog.java:524)
at com.test.testing.TestDialog.d(TestDialog.java:519)
at com.test.testing.g.onClick(TestDialog.java:648)
at android.view.View.performClick(View.java:3620)
at android.view.View$PerformClick.run(View.java:14292)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4507)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
at dalvik.system.NativeStart.main(Native Method)

我无法重现此错误,但我收到了很多错误报告.

I can't reproduce this error, but I am getting a lot of error reports.

谁能帮我解决这个错误?

Can anybody help that how can I fix this error?

推荐答案

这很常见 问题.我们通过在 DialogFragment 扩展类中重写 show() 和处理异常解决了这个问题

This is common issue. We solved this issue by overriding show() and handling exception in DialogFragment extended class

public class CustomDialogFragment extends DialogFragment {

    @Override
    public void show(FragmentManager manager, String tag) {
        try {
            FragmentTransaction ft = manager.beginTransaction();
            ft.add(this, tag);
            ft.commit();
        } catch (IllegalStateException e) {
            Log.d("ABSDIALOGFRAG", "Exception", e);
        }
    }
}

请注意,应用此方法不会改变 DialogFragment.class 的内部字段:

Note that applying this method will not alter the internal fields of the DialogFragment.class:

boolean mDismissed;
boolean mShownByMe;

在某些情况下,这可能会导致意外结果.最好使用 commitAllowingStateLoss() 而不是 commit()

This may lead to unexpected results in some cases. Better use commitAllowingStateLoss() instead of commit()

这篇关于在显示对话框时,我得到“在 onSaveInstanceState 之后无法执行此操作";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:调用android对话框而不褪色背景 下一篇:Android - 如何在本机屏幕上显示对话框?

相关文章