6

How to restore the dialog etc after rotating the screen? For example, pop up an alertDialog to tell user some information. then the user rotate the screen to another orientation. How to restore the alertDialog? Can any one guide me to do it? Thanks!

Appended later:

I looked into the android source code and find these things:

Dialogs are stored in mManagedDialogs, and the related information is:

mManagedDialogs = new SparseArray<ManagedDialog>();

onSaveInstanceState related:

final void performSaveInstanceState(Bundle outState) {
    onSaveInstanceState(outState);
    saveManagedDialogs(outState);
}

In saveManagedDialogs, it has something to do with mManagedDialogs.

onRestoreInstanceState related:

final void performRestoreInstanceState(Bundle savedInstanceState) {
    onRestoreInstanceState(savedInstanceState);
    restoreManagedDialogs(savedInstanceState);
}

In restoreManagedDialogs, it has something to do with mManagedDialogs.

As you can see, for advanced feature, you must do the saving and restoring job by yourself. It may be a night mare when you have tons customized dialogs. I have not tried the complex dialog (has input EdiText, listView, say). In this way, I'd like to warn users: Never rotate the screen when inputing your info in the dialog... OR, lock the rotation dynamically when showing the dialog.

Thanks for all the people who answered me. Hope my information help you too.

Regexident
  • 29,441
  • 10
  • 93
  • 100
Henry Sou
  • 882
  • 2
  • 12
  • 19

3 Answers3

5

It is handled for you as long as you use Activity#showDialog to show it and Activity#onCreateDialog to create it: http://developer.android.com/reference/android/app/Activity.html#showDialog%28int%29

http://developer.android.com/reference/android/app/Activity.html#onCreateDialog%28int,%20android.os.Bundle%29

Lance Nanek
  • 6,377
  • 2
  • 24
  • 19
-5

Add like this in your activity tag in manifest

<activity android:label="@string/app_name" android:configChanges="keyboardHidden|orientation|screenSize" android:name=".your.package"/>
Deepak
  • 61
  • 1
  • 4
-18

The approach I took was to not allow the OS to restart your activity after the layout configuration change. To accomplish this, add this line within activities that you want to prevent from restarting in your manifest file:

 <activity
 android:configChanges="orientation|keyboard"
 ...
 >

Optionally, you can handle the configuration change in code in case there are some layout changes you want to make manually, such as reloading a new view from XML. This is done by overwriting the onConfigurationChanged() method in your Activity class:

@Override
public void onConfigurationChanged(Configuration newConfig)
{
    //Handle config changes here, paying attention to
    //the newConfig.orientation value
    super.onConfigurationChanged(newConfig);
}

Edit: Added "|keyboard" to the list of config changes

Aaron C
  • 3,328
  • 1
  • 24
  • 22
  • No, this change to the manifest file simply stops Android from restarting your activity when that particular configuration change (orientation) occurs. It will not stop the screen from rotating. You might also want to add 'keyboard' for the phones with a slide out keyboard, as that will also trigger a restart. – Aaron C Sep 13 '10 at 12:09
  • You are right! I added android:configChanges="orientation|keyboard" and it works! Thank you very much! – Henry Sou Sep 13 '10 at 23:31
  • 15
    Works but not recommended. – Ed Burnette Nov 09 '11 at 21:58
  • @EdBurnette care to expand Ed? – barry Jun 02 '12 at 20:15
  • Android devs recommend that, instead of implementing onConfigurationChanged you handle the activity destroy and create correctly. Fragments are the best way because they can outlive their containing activities. – Ed Burnette Sep 13 '12 at 22:33