简体   繁体   English

在Android上进行简单的屏幕旋转

[英]simple screen rotation on Android

I've looked for how to not reload whole activity when rotating screen in android app. 我一直在寻找如何在android应用中旋转屏幕时不重新加载整个活动。 Finally, I figured out that using only 最后,我发现仅使用

android:configChanges="orientation"

in activity definition in AndroidManifest is really enough. 在AndroidManifest中定义活动就足够了。 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. 当我旋转手机时,不会重新加载活动,我的所有视图都已完美调整为横向,没有错,没有触发onSaveInstanceState或onRestoreInstanceState(在一些程序旋转指南中使用)。

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/ 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 在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. 对于“简单”的应用程序,例如“联系人选择器”或诸如此类,您实际上不需要保存信息,实际上只需要重新定向视图即可,因此使用android:configChanges =“ orientation”即可完成工作。

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. 顺便说一句,我既不是Android专家,也不是其他任何人,这只是我的简单观点/理解。 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. 但是,假设您已定义了景观资源,那么现在需要自己处理UI资源的交换。 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:- 另外,如果清单文件中的目标是SDK 13或更高版本,则警告您必须包括screenSize值以进行轮换更改,例如:

android:configChanges="orientation|screenSize"

If you're targeting SDK 12 or below, it's fine as you've got it. 如果您的目标是SDK 12或更低版本,那么就可以了。

adding this in the manifestfile 在清单文件中添加

android:configChanges="keyboardHidden|orientation"

this is really enough. 这真的足够了。 i also hav same doubt 我也有同样的疑问

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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