Orientation Issues

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();
    // enabling listening
    listener.enable();
}
    @Override
protected void onPause() {
    super.onPause();
    // disabling listening
    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();

    }
}

}

+5
3

. . , .

, , , . "" , , , .

. .

+5

onConfigurationChanged?

@Override
public void onConfigurationChanged(Configuration newConfig) {
 if(newConfig.equals(Configuration.ORIENTATION_LANDSCAPE)…
+3

Add android: configChanges = "orientation" in the manifest file in the activity tag, for example

<activity android:label="@string/app_name" android:configChanges="orientation" android:name=".com.androidpeople">

+1
source

All Articles