When I change the orientation o开发者_如何学运维f the Android application, it calls onStop method and then onCreate. How to avoid caling onStop and onCreate when orientation changes?
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
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?
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.
精彩评论