onCreate method is called every time on Activity whenever screen is rotated. Is it just onCreate being called again or whole activity is re-created?
2 Answers
It is not just onCreate(). When the screen is rotated, the activity is paused, stopped, and restarted. See this question for more info: Activity lifecycle - onCreate called on every re-orientation
If the question is "Why does this happen?" the answer has to do with functionality inside of Android's activities and windows. More specifically, android currently does not have a way to move, resize, and relayout each and every view when the orientation is changed. To make handling this scenario possible, the simpler implementation of just tearing down the activity and bringing it back up in a different orientation was implemented.

- 1
- 1

- 6,613
- 2
- 30
- 26
-
Thank you for the answer. Offtopic question: how can I send or save data between activities when screen orientation changes? And there is a twist, it can't be stored in database or file because it has sensative data. – jM2.me Oct 23 '12 at 06:03
-
@jM2.me see this question and feel free to post a new question if you have a new specific question http://stackoverflow.com/questions/151777/saving-activity-state-in-android – spatulamania Oct 24 '12 at 04:29
When the orientation change the onDistroy method will call which indicated the Activity is closed, and again new activity is created with new height and width.
when orientation change all the objects in the Activity class will destroy and when Activity relaunch they will create again, if there is large amount of data it takes more time to load all the data again.. so it is prefer to separate and store all the data in Non-Activity class and use in Activity class by creating objects to NonActivity class..
when the orientation change onSaveInstanceState method also called and
by using onSaveInstanceState the data will set or store some values when activity destroy and recreate
protected void onSaveInstanceState(Bundle icicle) {
super.onSaveInstanceState(icicle);
icicle.putLong("param", value);
}
when activity restarts the on create method call again and this time the Bundle returns the value which you saved in onSaveInstanceState
public void onCreate(Bundle icicle) {
if (icicle != null){
value = icicle.getLong("param");
}
}

- 4,291
- 2
- 26
- 45
-
So with onSaveInstanceState I can pass to the new Activity that is being created because of rotation? Can I pass a list from old activity to new using onSaveInstanceState? – jM2.me Nov 01 '12 at 01:09
-
1You have to call the super.onSaveInstanceState(bundle) method before adding your values to the Bundle. – goetz Oct 12 '16 at 23:04
-
Oh My God, this destroyed my day. I'm new to android and I just discovered I need to write a sea of code just to allow a screen rotation. Very very bad – Mike97 Oct 20 '20 at 20:04