Problems when handling orientation changes
Posted
by nixau
on Stack Overflow
See other posts from Stack Overflow
or by nixau
Published on 2010-06-07T08:42:27Z
Indexed on
2010/06/07
10:32 UTC
Read the original article
Hit count: 349
Hi all,
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();
}
}
}
© Stack Overflow or respective owner