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

时间:2017-01-11 08:33:04

标签: android

我已经读过这些问题 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)

并追踪enter image description here

enter image description here

1 个答案:

答案 0 :(得分:0)

如果您尝试显示的对话框是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所述,这是最后的选择。这篇文章深入解释了你的问题。