5

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?

indira
  • 6,569
  • 18
  • 65
  • 80

3 Answers3

8

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

Aidan
  • 5,346
  • 2
  • 19
  • 18
Karri Rasinmäki
  • 1,019
  • 10
  • 19
2

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?

Community
  • 1
  • 1
sdabet
  • 18,360
  • 11
  • 89
  • 158
  • 1
    Well, 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
0

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.

See also this and that article on the developer pages.

Heiko Rupp
  • 30,426
  • 13
  • 82
  • 119
  • 1
    This 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