有时显示对话框得到“onSaveInstanceState 后无法执行此操作”

Posted

技术标签:

【中文标题】有时显示对话框得到“onSaveInstanceState 后无法执行此操作”【英文标题】:Show dialog some time get "Can not perform this action after onSaveInstanceState" 【发布时间】:2017-05-25 22:58:55 【问题描述】:

我已经阅读了这个问题 On showing dialog i get "Can not perform this action after onSaveInstanceState"

Show DialogFragment when user clicks deny on Runtime permissions dialog

但我仍然不知道如何解决这个问题?有人帮我吗?如果信息不够详细,我会提供更多!

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

java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
    at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManagerImpl.java)
    at android.support.v4.app.FragmentManagerImpl.popBackStackImmediate(FragmentManagerImpl.java)
    at android.support.v4.app.FragmentActivity.onBackPressed(FragmentActivity.java)
    at android.app.Activity.onKeyUp(Activity.java:2531)
    at android.view.KeyEvent.dispatch(KeyEvent.java:2731)
    at android.app.Activity.dispatchKeyEvent(Activity.java:2784)
    at android.support.v7.app.AppCompatActivity.dispatchKeyEvent(AppCompatActivity.java)
    at android.support.v7.view.WindowCallbackWrapper.dispatchKeyEvent(WindowCallbackWrapper.java)
    at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.dispatchKeyEvent(AppCompatDelegateImplBase.java)
    at com.android.internal.policy.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:2402)
    at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:4659)
    at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4615)
    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4110)
    at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4163)
    at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4129)
    at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4293)
    at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4137)
    at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4350)
    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4110)
    at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4163)
    at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4129)
    at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4137)
    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4110)
    at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4163)
    at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4129)
    at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4326)
    at android.view.ViewRootImpl$ImeInputStage.onFinishedInputEvent(ViewRootImpl.java:4496)
    at android.view.inputmethod.InputMethodManager$PendingEvent.run(InputMethodManager.java:2330)
    at android.view.inputmethod.InputMethodManager.invokeFinishedInputEventCallback(InputMethodManager.java:1934)
    at android.view.inputmethod.InputMethodManager.finishedInputEvent(InputMethodManager.java:1925)
    at android.view.inputmethod.InputMethodManager$ImeInputEventSender.onInputEventFinished(InputMethodManager.java:2307)
    at android.view.InputEventSender.dispatchInputEventFinished(InputEventSender.java:141)
    at android.os.MessageQueue.nativePollOnce(MessageQueue.java)
    at android.os.MessageQueue.next(MessageQueue.java:328)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:5769)
    at java.lang.reflect.Method.invoke(Method.java)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)

并追踪

【问题讨论】:

【参考方案1】:

如果您尝试显示的对话框是 DialogFragment,那么您一定是这样调用的

messageDialogFragment.show(getSupportFragmentManager(), TAG);

您可以改为执行以下操作:

FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.add(this, tag);
ft.commitAllowingStateLoss();
messageDialogFragment.show(ft, TAG);

OnSaveInstanceState 后您将永远不会出现无法执行此操作的错误” 但正如这篇伟大的文章http://www.androiddesignpatterns.com/2013/08/fragment-transaction-commit-state-loss.html 中所述,这是最后的手段。文章深入解释了您的问题。

【讨论】:

ChooseSIMDialogActivity 是扩展 AppCompatActivity,任何想法。

以上是关于有时显示对话框得到“onSaveInstanceState 后无法执行此操作”的主要内容,如果未能解决你的问题,请参考以下文章

为啥显示自绘无模对话框时会出现任务栏? .. 有时?

iOS 模拟器崩溃 requestAlwaysAuthorization()

有时,BottomSheetDialogFragment 在屏幕中心打开,如警报对话框

非续订订阅已购买警报轨道

一个具有两种显示类型的片段[关闭]

jQuery UI 对话框中的 IFrame 有时不会在 Chrome 中加载内容,直到对话框被移动或调整大小(但适用于 FF 和 Edge)