How to avoid activity re-creation when being rotated, while also respecting orientation lock?

天大地大妈咪最大 提交于 2019-12-02 12:59:48

问题


Background

Each time the device is being rotated, Android will destroy the current activity and re-create it with the new configuration.

An easy way to avoid activity re-creation is by adding "configChanges" values to the manifest ("screenSize" and "orientation" as I recall).

The problem

If you use the "configChanges" attribute, the app won't respect the orientation lock feature that some (or all?) Android devices have, which prevent anything from being rotated when the screen is being rotated.

What I've tried

I tried playing with the "screenOrientation" attribute values, yet none of them achieved what I wanted.

The question

How can I prevent the activity from being re-created when the screen changes its orientation, while also respecting the user preference of orientation-lock?

Is it perhaps possible to get the value of this preference, so that I could let the activity lock its orientation when it's being set?


回答1:


OK, never mind. I think I just missed the correct one:

android:screenOrientation="user"

Together with the other configChanges values, this works perfectly.



来源:https://stackoverflow.com/questions/24000361/how-to-avoid-activity-re-creation-when-being-rotated-while-also-respecting-orie

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!