问题描述
我需要在我的 Android 应用程序中处理方向更改.为此,我决定使用 OrientationEventListener
便利类.但是他的回调方法被赋予了一些奇怪的行为.
我的应用程序以纵向模式启动,然后最终切换到横向模式.我在回调 onOrientationChanged
方法中执行了一些自定义代码,该方法提供了一些额外的 UI 处理逻辑 - 它有一些对 findViewById
的调用.奇怪的是,当从横向模式切换回纵向模式时,onOrientationChanged
回调被调用了两次,更糟糕的是 - 第二次调用处理的是 bad Context
- findViewById
方法开始返回 null
.这些调用直接从 MainThread
@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);监听器 = 新的方向监听器();}@覆盖受保护的无效 onResume() {超级.onResume();//启用监听listener.enable();}@覆盖受保护的无效 onPause() {超级.onPause();//禁用监听listener.disable();}
我已经用一个虚拟的 Activity
复制了相同的行为,除了处理方向处理的逻辑之外没有任何逻辑.我通过按 Ctrl+F11 从 Android 2.2 模拟器启动方向切换有什么问题?
更新:实现 OrientationEventListener
私有类 OrientationListener 扩展 OrientationEventListener {公共方向L(){超级(getBaseContext());}@覆盖公共无效onOrientationChanged(int方向){toString();}}
}
这只是模拟器中记录的错误.真实设备不会表现出这种双生命周期事件行为.不久前我遇到了同样的问题,但它在真实设备上消失了.
如果可以的话,我建议忽略这个问题,只测试一个方向的方向变化,直到你拿到一部实体手机.否则,您可以通过保持一个静态布尔值来跳过"第二组生命周期调用,以指示您已经完成了第一组.
有关详细信息,请参阅此问题报告.p>
I need to handle orientation changes in my Android application. For this purpose I decided to use OrientationEventListener
convenience class. But his callback method is given somewhat strange behavior.
My application starts in the portrait mode and then eventually switches to the lanscape one. I have some custom code executing in the callback onOrientationChanged
method that provides some additional UI handling logic - it has a few calls to findViewById
.
What is strange is that when switching back from landscape to portrait mode onOrientationChanged
callback is called twice, and what's even worse - the second call is dealing with bad Context
- findViewById
method starts returning null
. These calls are made right from the MainThread
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
listener = new OrientationListener();
}
@Override
protected void onResume() {
super.onResume();
// enabling listening
listener.enable();
}
@Override
protected void onPause() {
super.onPause();
// disabling listening
listener.disable();
}
I've replicated the same behavior with a dummy Activity
without any logic except for one that deals with orientation hadling.
I initiate orientation switch from the Android 2.2 emulator by pressing Ctrl+F11
What could be wrong?
Upd:
Inner class that implements OrientationEventListener
private class OrientationListener extends OrientationEventListener {
public OrientationL() {
super(getBaseContext());
}
@Override
public void onOrientationChanged(int orientation) {
toString();
}
}
}
This is a documented bug in the emulator ONLY. A real device will not exhibit this double-lifecycle-events behavior. I had the same issue a while ago and it disappears on a real device.
I would suggest ignoring the problem if you can by only testing orientation changes in one direction until you get your hands on a physical phone. Otherwise you might be able to "skip" the second set of lifecycle calls by keeping a static boolean around indicating you've already gone through the first set.
See this issue report for more info.
这篇关于处理方向变化时的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!