Jorge Gil
Jorge Gil

Reputation: 4355

Android - Best way of avoiding Dialogs to dismiss after a device rotation

After a long search around this forum, I found a lot of answers where people propose using solutions for avoiding dialogs to dismiss after rotation, just like:

android:configChanges="keyboardHidden|orientation"

Or override the following method, which appeared to be the most recommended:

protected Dialog onCreateDialog(int id)

But, after look around the Android Reference Documentation, I could notice that these Dialog methods are being deprecated.

So, the obvious question is:

Today, what is the best way of avoiding Dialogs to dismiss after a device rotation?

Thanks in advance.

Upvotes: 5

Views: 12090

Answers (3)

Budius
Budius

Reputation: 39836

what I'm gonna answer is based on Dialogs alone (NOT dialogfragment which are a whole different game).

Dialogs are part of the activity, and as such, they are being destroyed during the rotation. References that you used to have to the dialog will now point to a dialog that it's not on screen anymore, and likely to cause you problems.

Unfortunately there's no easy solution. With android:configChanges="keyboardHidden|orientation" you'll be creating an array of other problems for yourself.

The way to do is to save any configuration of the dialog, dismiss it, and whenever the activity is being re-created, re-create the dialog.

Upvotes: 6

Anoop
Anoop

Reputation: 993

try this one....

onOrientationChanged(int x)
{
 dialogobject.dismissDialog();
}

Upvotes: 0

Vladimir Ivanov
Vladimir Ivanov

Reputation: 43098

You should now use DialogFragment from new Fragments API. To use it on the platform lower, than 3.0, use compatibility package.

Upvotes: 7

Related Questions