0

I have added android:screenOrientation="sensorLandscape" to an activity in my Manifest. And there is no android:configChanges attribute.

This seems like a bug to me, but now my activity is not being re-created i.e. onCreate() is not being called when the device is rotated.

Also, onConfigurationChanged() is not being called either.

Removing the line android:screenOrientation="sensorLandscape" fixes the problem and the activity is restarted as expected.

Can someone confirm it is a bug, and/or is there a workaround for it?

Vedant Agarwala
  • 18,146
  • 4
  • 66
  • 89

2 Answers2

2

Can someone confirm it is a bug...

This is not a bug. It's just how they designed it. According to Dianne Hackborn, from this post on Google Groups (about half-way down):

This is simply not a configuration change. There is no notification the platform provides for when it does this, because it is invisible to the environment the app is in.


...is there a workaround for it?

A possible workaround would be to register some Sensors to detect orientation change, but that is a little more work than it was since the Orientation Sensor was deprecated. You'd need a Magnetic Field Sensor and an Accelerometer to replace its functionality. This post demonstrates using these Sensors to get orientation values.

Community
  • 1
  • 1
Mike M.
  • 38,532
  • 8
  • 99
  • 95
  • I don't understand, if landscape -> reverese landscape is not a configuration change then why does the value in `getWindowManager().getDefaultDisplay().getRotation()` change? Shouldn't `onConfigurationChanged` be called because something in the configuration has changed? But it's considered a design decision to not inform the user when the screen has flipped? I now have upsideddown camera view and no screen orientation callback, only solution is check `getRotation()` every frame? This is very frustrating and makes Android much less useful. – Logic1 Mar 31 '22 at 11:13
-1

I solved this issue by listening to the accelerometer events and keeping a field mCurrentDisplayRotation. Full code to add in your Activity to get it working:

private SensorManager mSensorManager;
private Sensor mAccelerometer;

private int mCurrentDisplayRotation = -1;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_camera );

    mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
    mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
}

protected void onResume() {
    super.onResume();
    mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
}

protected void onPause() {
    super.onPause();
    mSensorManager.unregisterListener(this);
}

public void onAccuracyChanged(Sensor sensor, int accuracy) {
}

public void onSensorChanged(SensorEvent event) {
    int rotation = getWindowManager().getDefaultDisplay().getRotation();
    if (mCurrentDisplayRotation != rotation) {
        mCurrentDisplayRotation = rotation;
        // handle the rotation change here
    }
}
Vedant Agarwala
  • 18,146
  • 4
  • 66
  • 89