Activity not losing its state when I rotate the emulator screen
I have created one layout with two edit boxes. After I launch my application, I enter some text in both of the edit boxes, and I press the 7 and 9 buttons to rotate the emulator screen, and the text I have entered still remains. But I have read from the docs that upon screen orientation change, the activity will be restarted and the data will be gone if I don't save the state.
Am I doing something wrong to change the screen orientation? I welcome any sugges开发者_开发问答tions, as I am new with Android.
As long as you give the EditText an unique ID, Android is smart enough to retain the text for you
Quote from Mark Murphy here:
Android will automatically handle the contents of EditText on an orientation change if you use all of the defaults and have all uniquely-ID'd widgets.
Source
You can set behaviour of your activity as per configuration of the phone, onConfigurationChanged (Configuration newConfig) is listner by which you can set behaviour of your phone. http://developer.android.com/reference/android/app/Activity.html#onConfigurationChanged(android.content.res.Configuration) for more details...
The activity is (normally) recreated when the screen orientation changes. However, before the old views are destroyed the onSaveInstanceState
method is called, and after the new ones are created, the onRestoreInstanceState
method is called. This allows the edit boxes to save their old content, and reload it after the new activity is created.
If you don't want the activity to be recreated, use the onConfigurationChanged
listener and android:configChanges
attribute.
精彩评论