Reputation: 653
We're using Dialogfragment to show some dialogs and sometimes these cause an "IllegalStateException: Can not perform this action after onSaveInstanceState".
In the past this happened frequently and I was able to reduce the number of this IllegalStateException by writing a DialogHelper to show/dismiss dialogs.
Unfortunately, sometimes I still get crash reports about this and had another look at it and I'm just not able to find out how to finally fix this problem correctly.
The reported crashes at the moment are happening onClick of a button and we want to show a dialog (for example DateSliderDialog) but I can't reproduce the Crash :/.
mFromDateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!isFinishing()) {
String title= getString(R.string.dateSliderTitle);
DateSliderDialog dateSliderDialog= DateSliderDialog.newInstance(title, getMinFromDate().getTimeInMillis(), getFromDateForDialog().getTimeInMillis(), DateUtils.getEndOfUniverseDate().getTime());
DialogHelper.showDialog(getSupportFragmentManager(), dateSliderDialog, InquireReservationActivity.this, String.valueOf(FROM_DATE_DIALOG_ID));
}
}
});
which calls DialogHelper.showDialog(...) method
public static void showDialog(FragmentManager fragmentManager, DialogFragment dialogFragment, Context context, String dialogTag) {
if (fragmentManager != null && dialogFragment != null && (dialogFragment.getDialog() == null || !dialogFragment.getDialog().isShowing())) {
if (context instanceof MyActivity) {
if(!((MyActivity)context).isFinishing() && ((MyActivity)context).mIsActivityRunning) {
dialogFragment.show(fragmentManager, dialogTag);
}
} else if (context instanceof MyListActivity) {
if(!((MyListActivity)context).isFinishing() && ((MyListActivity)context).mIsActivityRunning) {
dialogFragment.show(fragmentManager, dialogTag);
}
} else if (context instanceof MyPreferenceActivity) {
if(!((MyPreferenceActivity)context).isFinishing() && ((MyPreferenceActivity)context).mIsActivityRunning) {
dialogFragment.show(fragmentManager, dialogTag);
}
} else {
if(!((Activity)context).isFinishing()) {
dialogFragment.show(fragmentManager, dialogTag);
}
}
}
}
As you can see, I'm already checking for:
Does anyone have a clue why this is still happening and how to fix it?
Thanks for your suggestions and answers
Upvotes: 2
Views: 397
Reputation: 777
Whenever you are dismissing the dialog, use
dialogFragment.dismissAllowingStateLoss();
Upvotes: 3