3

I would like for my activity not to rotate when the device is turned. Using

android:screenOrientation="nosensor"

does disable orientation changes, but with one caveat: the activity switches to portrait mode. I just want it to keep the current orientation (e.g., if the screen was in landscape when the activity was started, then stay in landscape mode even when the device is rotated). This is not what "nosensor" seems to be doing. It seems to simply be the exact same behavior as "portrait". Am I using it wrong?

I've tried using setRequestedOrientation( getRequestedOrientation ), but if the current requested orientation is undefined, then my activity is going to rotate. I just want too "lock" the effective screen rotation.

user1924406
  • 111
  • 1
  • 8

1 Answers1

3

What you can do is to tell Android that you are going to handle the orientation configuration change on your own. You do it by specifying orientation for the android:configChanges attribute of the activity tag.

<activity android:name=".MyActivity"
          android:configChanges="orientation"
          android:label="@string/app_name">

See this link for more information.
http://developer.android.com/guide/topics/resources/runtime-changes.html#HandlingTheChange

As to why nosensor would not work, is because it's mentioned as below in the documentation.

The orientation is determined without reference to a physical orientation sensor. The sensor is ignored, so the display will not rotate based on how the user moves the device. Except for this distinction, the system chooses the orientation using the same policy as for the "unspecified" setting.

and as to what unspecified is in the documentation

The default value. The system chooses the orientation. The policy it uses, and therefore the choices made in specific contexts, may differ from device to device.

Ranhiru Jude Cooray
  • 19,542
  • 20
  • 83
  • 128
  • Once I've overridden `onConfigurationChanged`, how do I prevent the screen from rotating? As far as I can see, it allows me to do extra stuff when the config changes, but not to replace the existing behaviour. If I remove the call to `super.onConfigurationChanged`, I get an Exception. What I'm after is something like http://stackoverflow.com/questions/6599770/screen-orientation-lock?rq=1 that works reliably. – user1924406 Jan 30 '13 at 13:18
  • Have you tried keeping the `android:configChanges="orientation` in the tag but **not** overriding the `onConfigurationChanged` event? – Ranhiru Jude Cooray Jan 31 '13 at 12:36
  • Yes. This amounts to letting `super.onConfigurationChanged` handle the event, which means the display will be rotated. – user1924406 Feb 03 '13 at 02:53