When I change the orientation of the Android application, it calls onStop method and then onCreate. How to avoid caling onStop and onCreate when orientation changes?
3 Answers
It have been long time since this question have been active, but I sill need to answer this. I had same issue and found answer.
In manifest you should add android:configChanges=orientation|screenLayout|layoutDirection|screenSize
in your activity which should not call default actions on orientation change.
...
<activity android:name=".Activity"
android:screenOrientation="portrait"
android:configChanges="orientation|screenLayout|layoutDirection|screenSize"
...
And in your activity:
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// This overrides default action
}
Original example:
http://android-er.blogspot.fi/2011/05/prevent-activity-restart-when-screen.html

- 5,346
- 2
- 19
- 18

- 1,019
- 10
- 19
-
3I had to add "|screenSize" as well for more recent OSs. – Jake Brownson Feb 06 '14 at 19:48
-
Yes, basically add any parameter that might be related to or caused by orientation change. – Karri Rasinmäki Feb 07 '14 at 20:54
-
1Edited answer so that "screenSize" is in parameter list as well. – Karri Rasinmäki Mar 03 '15 at 11:43
This is the default behavior: the activity is recreated when orientation changes. But you can decide to ignore this event. You can find details here : How to make an application ignore screen orientation change?
-
1Well, the technique in the linked article apparently prevents orientation changes, but not the calls to the callbacks when the orientation changes (which is what was asked). – Heiko Rupp Feb 25 '12 at 12:26
You can't avoid that, those are system callbacks. You can save the state though in onStop and then have the system pass it to onCreate for a faster recovery of the state after the screen orientation has changed.

- 30,426
- 13
- 82
- 119
-
1This can avoided by calling android:configChanges="orientation|keyboardHidden" . But still there are some issues – indira May 12 '11 at 06:53
-
Well, this apparently prevents orientation changes, but not the calls to the callbacks when the orientation changes (which is what was asked). – Heiko Rupp Feb 25 '12 at 12:25