Android: restore dialogue, etc. After changing the rotation

How to restore dialogue, etc. after turning the screen? For example, run alertDialog to tell the user some information. then the user rotates the screen to a different orientation. How to restore alertDialog? Can someone help me do this? Thanks!

Added later:

I looked at the Android source code and found the following things:

Dialogs are stored in mManagedDialogs , and related information:

 mManagedDialogs = new SparseArray<ManagedDialog>(); 

onSaveInstanceState related:

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

In saveManagedDialogs it has something to mManagedDialogs with mManagedDialogs .

onRestoreInstanceState related:

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

In restoreManagedDialogs it has something to mManagedDialogs with mManagedDialogs .

As you can see, for an advanced function, you must perform backup and restore yourself. It can be a night mare when you have subtle, customizable dialogs. I have not tried a complex dialog (there is an input EdiText, listView, let's say). Thus, I would like to warn users: never rotate the screen when entering your information in a dialog ... OR, block the rotation dynamically when the dialog is displayed.

Thanks to all the people who answered me. I hope my information helps you too.

+6
android rotation dialog restore
source share
3 answers

The approach I took was to prevent the OS from restarting your activity after changing the layout configuration. To do this, add this line to the actions you want to prevent restarting in the manifest file:

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

Optionally, you can handle the configuration change in the code if there are some layout changes that you want to make manually, for example, reloading a new view from XML. This is done by overwriting the onConfigurationChanged () method in the 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 configuration changes

-sixteen
source share
+5
source share

Add manifest activity to your shortcut

 <activity android:label="@string/app_name" android:configChanges="keyboardHidden|orientation|screenSize" android:name=".your.package"/> 
-5
source share

All Articles