5

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.

Jorge Gil
  • 4,265
  • 5
  • 38
  • 57
  • any easy way to do that is lock screen orientation so there is no rotation, but I am guessing that isn't an option. – MikeIsrael Aug 15 '12 at 06:54

3 Answers3

7

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

Vladimir Ivanov
  • 42,730
  • 18
  • 77
  • 103
  • Thank you @Vladimir. I will try with DialogFragments, since I just saw the next state in documentation: _Dialog onCreateDialog(int id, Bundle args) This method is deprecated. **Use the new DialogFragment class** with FragmentManager instead; this is also available on older platforms through the Android compatibility package._ Now, I'm pretty sure this is the best way. – Jorge Gil Aug 15 '12 at 16:56
  • 2
    The same answer with some example code: http://stackoverflow.com/a/15729764/842697 – Brais Gabin Mar 31 '13 at 13:24
5

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.

Jorge Gil
  • 4,265
  • 5
  • 38
  • 57
Budius
  • 39,391
  • 16
  • 102
  • 144
0

try this one....

onOrientationChanged(int x)
{
 dialogobject.dismissDialog();
}
Anoop
  • 993
  • 6
  • 16