onConfigurationChanged not called for 180 and 270 degrees

Viewed 1202

I have faced a strange issue (or maybe unexpected behavior) on my Android device.

The problem is that I am listening for configuration changes in my DialogFragment like this:

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    // Do something secret here :)
}

I added android:configChanges to the Activity which is responsible for showing the dialog fragment

<activity
    android:name=".SecretActivity" 
    android:configChanges="orientation|screenSize"
    android:windowSoftInputMode="adjustResize" />

and indeed I am getting callbacks from the system when I am rotating device, but not in all cases. As you can see in the picture onConfigurationChanged( ) called only when I am rotating 90 degrees, and also 360, in other cases it is not called.

Is this an expected behavior? If yes, how I can detect all rotations (90, 180, 270, 360)?

enter image description here

4 Answers

If you have an activity with unspecified screen orientation, devices generally ignore 180° rotation and only support one landscape direction.

To support all directions, add an explicit screenOrientation attribute to you activity manifest entry: user or sensor, depending on whether you want to support device orientation locking by user or not.

OrientationEventlistener won't work when the device isn't rotating/moving.

I find display listener is a better way to detect the change.

 DisplayManager.DisplayListener mDisplayListener = new DisplayManager.DisplayListener() {
    @Override
    public void onDisplayAdded(int displayId) {
       android.util.Log.i(TAG, "Display #" + displayId + " added.");
    }

    @Override
    public void onDisplayChanged(int displayId) {
       android.util.Log.i(TAG, "Display #" + displayId + " changed.");
    }

    @Override
    public void onDisplayRemoved(int displayId) {
       android.util.Log.i(TAG, "Display #" + displayId + " removed.");
    }
 };
 DisplayManager displayManager = (DisplayManager) mContext.getSystemService(Context.DISPLAY_SERVICE);
 displayManager.registerDisplayListener(mDisplayListener, UIThreadHandler);

onDisplayChanged triggers only exactly when getRotation() changes. OrientationEventListener on the other hand triggers constantly in real time and even before getRotation() changes, leading to completely wrong screen rotation values when doing a 180° turn from a perspective.

Hmm, check manifest, add this/modify

android:configChanges="screenSize|smallestScreenSize|screenLayout|orientation"

change this

<activity
android:name=".SecretActivity" 
android:configChanges="orientation|screenSize"
android:windowSoftInputMode="adjustResize" />

to this

<activity android:name=".SecretActivity" android:configChanges="screenSize|orientation|screenLayout|navigation"/>

and please checkout this answer the second answer i mean.

Related