简体   繁体   中英

alert box is in same state when screen orientation has changed in android

My Android app shows an alert box. If the user doesn't select a button in the alert box, I change the screen orientation which causes the alert box to disappear. How can I change the screen orientation without causing the alert box to disappear?

When you change orientation, your activity is destroyed and onCreate is called again.

Override onConfigurationChanged if you don't want this behavior - or even override this behavior by defining android:configChanges in your manifest-file.

Alternatively, save your dialog state on onSaveInstanceState and restore it on onRestoreInstanceState

Just add

android:configChanges="keyboardHidden|orientation"

to the activity tag of every activity that you don't want to restart on a change from landscape to portrait mode in your manifest.xml. This will cause the OS to rebuild your layout without destroying it before the rebuild. The oncreate method will not get called again and you don't loose the state of the activity. But be carefull this will only work if you use the same layout file for portrait and landscape mode.

See this question for more information on this topic

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM