1

I am wondering how to switch off the orientation sensor completely in my android app for certain activities.

Here is what I struggle with. Let's say I set the orientation in the onCreate (or onStart/onResume) method of an activity dynamically with the following command.

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

What I experience is that screen is appearing on the display noticeable faster if I hold the device in a manner that would result in landscape mode according to the orientation sensor.

So what it feels like is as if the activity first wants to set the layout in the orientation as the sensor would suggest and thereafter it switches according to the setRequestedOrientation method. I even sometimes see this on the screen as described in a related post android - unexpected brief orientation change at switch of activity.

Seems like I am too late with the setRequestedOrientation call. So I am wondering how to switch off the the orientation sensor completely to avoid a setting of the orientation prior to the setRequestedOrientation call. Or as alternative, how to call setRequestedOrientation in time. As I need this feature dynamically as decided during runtime (preference settings) I cant set the orientation fixed in the manifest.

Thanks a lot in advance for any hint around this issue.

martin

Edit: I forgot to mention, the manifest contains

android:configChanges="keyboardHidden|orientation|screenSize"
Community
  • 1
  • 1
dorjeduck
  • 7,624
  • 11
  • 52
  • 66
  • 1
    Have you tried the `android:screenOrientation` manifest attribute? Please check http://stackoverflow.com/a/4675801/1321873 also – Rajesh Sep 24 '12 at 14:32
  • thx for feedback Hauleth. As mentioned at the end of my question: As I need this feature dynamically as decided during runtime (preference settings) I cant set the orientation fixed in the manifest. – dorjeduck Sep 24 '12 at 14:39

2 Answers2

0

The performance impact happens because android kills your activity and then starts a new instance with the new configurations. a good solution to this would be to handle configuration changes manually. by adding android:configChanges="orientation" to your activity element in the manifest file Overriding onConfigurationChanged method in your activity to do the layout

another solution would be having tow activities , one works only in landscape the other in portrait by specifying orientation attribute in manifest file, and lunch the suitable one.

Mr.Me
  • 9,192
  • 5
  • 39
  • 51
  • Thanks Mr Me for the feedback - I forgot to mention and just added it now, android:configChanges="orientation" is already set in the manifest – dorjeduck Sep 24 '12 at 14:42
  • The thing is that there is a difference according to how i hold the device and that should not be if both in the prior activity and the new activity the orientation is set fixed. – dorjeduck Sep 24 '12 at 14:43
  • Ok , So override the onConfigurationChanged method in your activity and should get a performance boost , android will not kill your activity instead it will call the above method – Mr.Me Sep 24 '12 at 14:44
  • the performance difference is according to how i hold the device when starting the activity from another activity so I dont think onConfigurationChanged is involved here – dorjeduck Sep 24 '12 at 14:46
0

This is how you can set orientation at runtime as follows

int currentOrientation = getRequestedOrientation();

if (currentOrientation == Configuration.ORIENTATION_LANDSCAPE) {
   setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
}
else {
   setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
}
Ameer Moaaviah
  • 1,530
  • 12
  • 27