7

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

  • Can you please share `SecretActivity` code? – Mayur Gajra Jan 16 '19 at 14:40
  • @MayurGajra sure `class SecretActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_secret_activity) } override fun onConfigurationChanged(newConfig: Configuration?) { super.onConfigurationChanged(newConfig) } }` –  Jan 16 '19 at 15:05
  • @MayurGajra it's nothing fancy just a simple activity which overrides `onConfigurationChanged ` –  Jan 16 '19 at 15:06
  • That's really strange because I just tried that same code and it works fine for me,can you please check that in other device because it seems like an device specific issue to me. – Mayur Gajra Jan 16 '19 at 15:26
  • @MayurGajra Are you really sure that when you rotate device 4 times you see 4 times `onConfigurationChanged ` called? –  Jan 17 '19 at 08:50
  • Yes,Just checked in Nexus 9 and Redmi 4A it worked fine,did you try setting `keyboardHidden` flag in conjunction like `orientation|screenSize|keyboardHidden`? – Mayur Gajra Jan 17 '19 at 14:31
  • @MayurGajra yes I tried that as well. –  Jan 18 '19 at 08:25
  • Did u check out this question? It looks like there is a listener for screen orientation out there! https://stackoverflow.com/questions/20542756/whats-the-correct-way-to-detect-a-screen-rotation-from-0-to-180-or-90-to-270 – Catluc Jan 22 '19 at 11:36

4 Answers4

6

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.

laalto
  • 150,114
  • 66
  • 286
  • 303
3

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.

Vanshaj Daga
  • 2,145
  • 11
  • 18
1

Hmm, check manifest, add this/modify

android:configChanges="screenSize|smallestScreenSize|screenLayout|orientation"
hemen
  • 1,460
  • 2
  • 16
  • 35
1

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.

Pouya Danesh
  • 1,557
  • 2
  • 19
  • 36