问题描述
不,不是老问题的反刍,请耐心等待.我不想避免一般的活动娱乐(清单中没有 android:configChanges
),我不想永久修复我的方向(没有 android:screenOrientation
在manifest)只是因为我懒得实现实例保存.
No, not a regurgitation of the old question, please, bear with me. I don't want to avoid activity recreation in general (no android:configChanges
in manifest), I don't want to fix my orientation permanently (no android:screenOrientation
in manifest) just because I'm lazy to implement instance saving.
我有一个应用程序,用户可以进行三种可能的设置:1. 自动更改方向布局,按照正常方式,2. 固定纵向,3. 固定横向.在我的情况下这是有道理的,因为纵向和横向显示显示不同的功能,用户可能希望限制为一个.没有必要,但有可能.
I have an app with three possible settings the user can make: 1. automatically changing layout on orientation, as per normal, 2. fixed portrait, 3. fixed landscape. It makes sense in my case because the portrait and landscape displays show different functionality and the user might want to restrict to just one. Doesn't have to but has the possibility.
该应用程序运行良好.我阅读了 onCreate
中的首选项设置,如果我处于固定模式之一,则调用 setRequestedOrientation
.我让系统处理方向变化,我不要求自己处理变化.
The app works just fine. I read the preference setting in onCreate
and call setRequestedOrientation
if I'm in one of the fixed modes. I let the system handle the orientation changes, I don't ask for handling the changes myself.
唯一的性能问题是,例如,当应用在设备的纵向位置启动但固定为横向时,onCreate
将被调用两次,一次用于原始启动,一次用于 setRequestedOrientation.它完美无缺,我处理得很好,但有性能损失,活动出现明显延迟.(在manifest中修复了screenOrientation
,仅用于测试目的,启动看起来好多了,只需调用一次onCreate
).
The only performance problem is that when, for instance, the app is started in the device's portrait position but fixed to landscape, onCreate
will be called twice, once for the original startup, once for setRequestedOrientation
. It works flawlessly, I handle it perfectly but there is a performance penalty, the activity appears with an obvious delay. (With screenOrientation
fixed in the manifest, only for the purpose of testing, the startup looks much better, with only a single call to onCreate
).
所以,我正在寻找一种与清单 screenOrientation
设置等效的代码.我不能也不想在清单中指定它,但是从 onCreate
调用它对于性能来说已经有点晚了.
So, what I'm looking for is a kind of code equivalent of the manifest screenOrientation
setting. I can't and don't want to specify it in the manifest but calling it from onCreate
is already a bit late for performance.
推荐答案
你试过了吗?
<activity
android:name=".YourActivity"
android:configChanges="orientation|keyboard|keyboardHidden|screenSize|screenLayout|uiMode" />
清单文件中的此 configChanges 行应避免重新调用 onCreate 方法 :)
This configChanges line on the manifest file should avoid the re-calling to the onCreate method :)
这篇关于根据偏好锁定 Android 方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!