5

I've got an activity. This activity works in portrait mode or landscape mode. In landscape mode, I load a new layout to show a graph. To detect the orientation, I set up this in my manifest :

android:screenOrientation="sensor" android:configChanges="orientation"

It works perfectly, because when I flip my phone, the new layout is loaded. But, in lanscape mode, the user can disable/enable the rotation with a button (it's a switcher).

I wrote this code :

public void onConfigurationChanged(Configuration pNewConfig) {
    Log.v(DEBUG_TAG, "Current configuration changes");

    if (pNewConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        Log.v(DEBUG_TAG, "Screen orientation : LANDSCAPE");
        if (pNewConfig.orientation != mCurrentOrientation) {
            initTestLandscape();
            mCurrentOrientation = pNewConfig.orientation;
        }
    } else {
        Log.v(DEBUG_TAG, "Screen orientation : PORTRAIT");
        if (!mIsLandscapeLocking
                && pNewConfig.orientation != mCurrentOrientation) {
            initTestPortrait();
            mCurrentOrientation = pNewConfig.orientation;
        }
    }
    super.onConfigurationChanged(pNewConfig);
}

This code works, but when the user is in landscape mode and flips the phone to back in portrait mode and the lock is on, the lanscape layout flips in portrait mode and I don't want that!

I've added

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

but this code disables the onConfigurationChanged method and I can't detect if the user flips the phone again...

Can you help me?

Stefan
  • 5,203
  • 8
  • 27
  • 51
FinalSpirit
  • 569
  • 1
  • 6
  • 17
  • 4
    It's very simple... I call the setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); when the user lock the view and call setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR); when the user unlock... it's so easy, How do I not have thought of this solution ? Apologies for any inconveniences. (I can't aswer my question) – FinalSpirit Aug 21 '11 at 10:43
  • Worth noting is that the solution you've provided for yourself will still behave strangely if the user locks whilst the view is rotated to the right instead of the left. The view will flip upside down because you are locking to `SCREEN_ORIENTATION_LANDSCAPE` which only means on one side. You probably want `SCREEN_ORIENTATION_SENSOR_LANDSCAPE` which holds it in landscape but still listens to the sensor from the device if the user switches to the opposite side of the phone/tablet. – SDJMcHattie Feb 06 '14 at 22:13
  • Yes, you must use SCREEN_ORIENTATION_SENSOR_LANDSCAPE in place of SCREEN_ORIENTATION_LANDSCAPE but when i post this topic, i was on Android 8 which doesn't support SCREEN_ORIENTATION_SENSOR_LANDSCAPE – FinalSpirit Feb 10 '14 at 15:36
  • Good point! Sorry I hadn't realised how old this was when I posted my comment. – SDJMcHattie Feb 11 '14 at 14:55

1 Answers1

0

make changes in android:configChanges:-

  android:configChanges="orientation|screenSize|keyboard|keyboardHidden"

after changes you code should be work

pRaNaY
  • 24,642
  • 24
  • 96
  • 146