How to safely dismiss DialogFragment in onstop()?
Asked Answered
M

3

12

I need to dismiss DialogFragment in onStop() of an FragmentActivity if it is showing, this is what I did

if(mAlertDlg != null && mAlertDlg.getDialog() != null)
    mAlertDlg.dismiss();

But I usually got IllegalStateException. So please tell me why that code is wrong and what is the correct way to dismiss DialogFragment in onStop()? Thank you.

Montemontefiascone answered 30/5, 2013 at 17:7 Comment(3)
See this blog post about this topic for more information. In general you don't want to use the *****AllowingStateLoss() methods to address issues like this. The IllegalStateException is thrown because state loss will occur. Using these methods are just avoiding the exception... but the state loss will still remain likely causing a bug in your application.Siegfried
Thank you for that nice article but in my scenario I still can find a better way than using *AllowingStateLoss() method to dismiss dialog when Activity is not in foreground anymore. Can't dismiss() on onPause() and onStop() so what should I do?Montemontefiascone
The FragmentManager will dismiss the dialog automatically when the activity goes into the background and/or is finished. I can't see why it would be necessary to dismiss the dialog in onStop()... By that point the FragmentManager has saved the state of its Fragments and recorded it all as part of the Activity's state, so the dismiss call will have no effect. Either way, neither dialogs nor fragments can persist across multiple Activity instances... They will be removed from the Activity's window either way once the Activity is stopped/destroyed.Siegfried
G
25

You should use dialogFragment.dismissAllowingStateLoss(). As the documentation say for commitAllowingStateLoss():

"Like commit() but allows the commit to be executed after an activity's state is saved. This is dangerous because the commit can be lost if the activity needs to later be restored from its state, so this should only be used for cases where it is okay for the UI state to change unexpectedly on the user."

So for dismissAllowingStateLoss() is the same approach.

Godavari answered 30/5, 2013 at 18:27 Comment(1)
You can also check this post for more info about commitAllowingStateLoss() and dismissAllowingStateLoss().Plenty
S
9

If you want to dismiss a DialogFragment in onStop(), you probably don't want to use a DialogFragment but a classic Dialog instead.

The reason why DialogFragment exists is to allow a dialog to be restored automatically when the Activity is re-created. If you dismiss it in onStop(), it will never be restored.

Also, if you use dismissAllowingStateLoss(), the dismiss transaction may not be recorded properly in onSaveInstanceState() (as the name says, a state loss may occur), and this will result in the dialog being restored when the activity is re-created, and obviously that's not what you want.

Saucy answered 7/1, 2016 at 13:37 Comment(0)
A
3

Try using dismissAllowingStateLoss() instead of dismiss().

Alkyd answered 30/5, 2013 at 18:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.