user5699130
user5699130

Reputation:

onConfigurationChanged not called for 180 and 270 degrees

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

Upvotes: 7

Views: 1621

Answers (4)

hemen
hemen

Reputation: 1490

Hmm, check manifest, add this/modify

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

Upvotes: 1

laalto
laalto

Reputation: 152907

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.

Upvotes: 6

Vanshaj Daga
Vanshaj Daga

Reputation: 2165

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.

Upvotes: 3

Pouya Danesh
Pouya Danesh

Reputation: 1627

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.

Upvotes: 1

Related Questions