I need to handle orientation changes in an Android app. For this, I decided to use the convenience class OrientationEventListener. But his callback method has some weird behavior.
My application runs in portrait mode and then eventually switches to lanscape. I have some code running in a callback method onOrientationChangedthat provides some additional UI processing logic - it has several calls findViewById. The strange thing is that when you switch from landscape mode to portrait mode, the onOrientationChangedcallback is called twice, and even worse - the second call deals with the bad one Context - the findViewByIdmethod starts to return null. These calls are made directly from MainThread.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
listener = new OrientationListener();
}
@Override
protected void onResume() {
super.onResume();
listener.enable();
}
@Override
protected void onPause() {
super.onPause();
listener.disable();
}
I reproduced the same behavior with a mannequin Activitywithout any logic other than the one regarding orientation. I initiate the orientation switch from the Android 2.2 emulator by pressing Ctrl + F11. What could be wrong?
Upd:
, OrientationEventListener
private class OrientationListener extends OrientationEventListener {
public OrientationL() {
super(getBaseContext());
}
@Override
public void onOrientationChanged(int orientation) {
toString();
}
}
}