Some users are reporting, if they use the quick action in the notification bar, they are getting a force close.
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");
}
The error is *IllegalStateException: Can not perform this action after onSaveInstanceState*.
The code line where the IllegarStateException gets fired is:
snoozeDialog.show(fm, "snooze_dialog");
The class is extending "FragmentActivity" and the "SnoozeDialog" class is extending "DialogFragment".
Here is the complete stack trace of the error:
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?
If the dialog is not really important (it is okay to not-show it when the app closed/is no longer in view), use:
And open your dialog(fragment) only when we're running:
EDIT, PROBABLY BETTER SOLUTION:
Where onSaveInstanceState is called in the lifecycle is unpredictable, I think a better solution is to check on isSavedInstanceStateDone() like this:
This error appears to be occurring because input events (such as key down or onclick events) are getting delivered after
onSaveInstanceState
is called.The solution is to override
onSaveInstanceState
in your Activity and cancel any pending events.That mean you
commit()
(show()
in case of DialogFragment) fragment afteronSaveInstanceState()
.Android will save your fragment state at
onSaveInstanceState()
. So, if youcommit()
fragment afteronSaveInstanceState()
fragment state will be lost.As a result, if Activity get killed and recreate later the fragment will not add to activity which is bad user experience. That's why Android does not allow state loss at all costs.
The easy solution is to check whether state already saved.
Note: onResumeFragments() will call when fragments resumed.
please try to use FragmentTransaction instead of FragmentManager. I think the below code will solve your problem. If not, Please let me know.
EDIT:
Fragment Transaction
Please check this link. I think it will solve you queries.
Use the method showAllowingStateLoss instead of show
Enjoy ;)
Many views post high-level events such as click handlers to the event queue to run deferred. So the problem is that "onSaveInstanceState" has already been called for the Activity but the event queue contains deferred "click event". Hence when this event is dispatched to your handler
and your code does
show
the IllegalStateException is thrown.The simplest solution is to clean event queue, in
onSaveInstanceState