简体   繁体   中英

simple screen rotation on Android

I've looked for how to not reload whole activity when rotating screen in android app. Finally, I figured out that using only

android:configChanges="orientation"

in activity definition in AndroidManifest is really enough. When I rotate the phone, the activity doesn't reload, all my views are perfectly adjusted to landscape orientation, nothing goes wrong, neither onSaveInstanceState nor onRestoreInstanceState, which is used in some guides to programmatical rotation, is fired.

But if it is that simple, why all tutorials and guides I found present so complicated solution? Ie

http://www.devahead.com/blog/2012/01/preserving-the-state-of-an-android-webview-on-screen-orientation-change/

Saving some data on orientation change in Android

By my experiments, everything is automatically correctly re-rendered, but isn't there some hidden problem with my simple solution I don't know about? Is it really that easy and correct?

Thank you

For "simple" apps such as a Contact Picker or something like that you don't really need to save information, you really just need to re-orientate the views, so using android:configChanges="orientation" will do the job.

But, if you're developing something like a game or anything that needs to calculate screen area and it'll be diferent in potrait/landscape mode you need to save this information and recalculate everything to display it correctly, without errors.

By the way, I'm no Android Expert or whatever, this is just my simple point of view/understanding. Feel free to downvote me if this is not the correct answer.

This is fine if you don't need to change any resources when the configuration changes. But say you have landscape resources defined, then you'll now need to handle swapping the UI resources yourself. But if the Activity needs no resources changed, it really is that simple.

Also, be warned if you target SDK 13 or above in your manifest you must include the screenSize value for rotation changes, such as:-

android:configChanges="orientation|screenSize"

If you're targeting SDK 12 or below, it's fine as you've got it.

adding this in the manifestfile

android:configChanges="keyboardHidden|orientation"

this is really enough. i also hav same doubt

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM