kumari
kumari

Reputation: 301

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?

Upvotes: 0

Views: 474

Answers (2)

Janusz
Janusz

Reputation: 189464

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

Upvotes: 1

DKIT
DKIT

Reputation: 3481

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

Upvotes: 1

Related Questions