Im new to android. I have written a couple of programs and tried them out on my phone. When I switch orientations its like my phone restarts the program. All my variables get reset. In fact the only thing that does not reset is the text that is in the edit text views. What causes this? How can I stop it? I have tried looking it on google and on stack overflow but all I am seeing is how to the view orientation from changing all together. I even tried, in one program, to set my variables with the get text method but this does not work.
-
1See my answer here http://stackoverflow.com/questions/6764086/setting-the-background-of-an-activity – Nikola Despotoski Mar 18 '12 at 01:59
5 Answers
When I switch orientations its like my phone restarts the program.
Well to be accurate, the currently visible Activity
is completely destroyed and recreated. Other components of your 'application' may or may not be impacted.
What causes this? How can I stop it?
It's by design and as for stopping it, you may or may not want to do that.
As has been mentioned, it is possible to specify that you want to handle 'configuration' changes (such as orientation) yourself or even force only one (e.g., landscape or portrait).
In many cases, however, an app developer may choose to change a layout based on whether a device is in one orientation or another. Some layouts may work fine in portrait but not in landscape (or vice versa) and the aim of the designed approach (to destroy / recreate the current visible Activity
) is meant to accommodate that.
If a dev chooses not to handle the configuration changes themself or force a specific orientation, the correct way to handle things is to make sure all data entered in 'volatile' UI elements (such as EditTexts) are correctly saved and recreated after orientation change.
To do this, understanding of the Activity
life-cycle is essential as is making use of the various Activity
methods that are called throughout the life-cycle to save / restore data.
Essential reading...

- 48,735
- 19
- 103
- 135
-
Thanks for the nice thorough answer and the terms! Makes complete since as to why. I have to be honest though. I get lost reading the developer pages and I do not understand most of what they are saying, unless I have a good basic knowledge and understanding before hand. I learn best from examples and tutorials. AND OF COURSE playing with methods. – JBreezy901 Mar 18 '12 at 03:37
-
@JBreezy901: I understand exactly what you mean about the developer pages. I was in the same place 16 months ago when I first started with Android and even now I've probably only worked my way through a very small percentage of what's there (and only stuff I've needed so far). The Application Fundamentals documentation along with that for the Activity and its lifecycle are definitely worth bookmarking. Glad I was able to shed some light, have fun. – Squonk Mar 18 '12 at 05:43
make sure you implement onSaveInstanceState
and be prepared to restore your activity from a Bundle
in onCreate
and you'll be all set. these will be called during rotation, so you'll have a new activity afterwards, but if you've saved your state and can restore it, nothing to worry about.

- 8,923
- 3
- 40
- 52
-
I still want my program to readjust its layout when switching from portrait and landscape. All that I want to be saved is my variables. Ints especially. Will this work? – JBreezy901 Mar 18 '12 at 03:27
-
yes, if you put all of your variables into that bundle, and then in your `onCreate` method retrieve them from the bundle instead of initializing your activity from scratch, you'll be all set. – dldnh Mar 18 '12 at 09:53
This is by design, onCreate
will be called when orientation is changed. If this is not desired, you can set it not to respond to orientation changes in AndroidManifest.xml
, thus your activity will not be recreated, setting android:screenOrientation="portrait" android:configChanges="orientation|keyboardHidden"
for your activity will restrict it to portrait mode.
For more info on the settings, see this.

- 11,760
- 8
- 55
- 73
Better add android:configChanges="keyboardHidden|orientation|screenSize" to
AndroidManifest.xmlin
activity

- 3
- 3
The ViewModel
class provides this feature:
ViewModel objects are automatically retained during configuration changes so that data they hold is immediately available to the next activity or fragment instance.

- 664
- 7
- 8