My application is designed to be "landscape-only". Rotation between left and right landscape must be supported so I am using android:screenOrientation="sensorLandscape"
.
However, there's a mini-game in my app when rotation must be disabled completely (because this mini-game is to be controlled by device tlting). After mini-game has finished, orientation handling policy must be restored back to sensorLandscape
.
The standard method to disable orientation change is to call
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
But not in the case! As mentioned here (and checked by me), it leads to orienation change to default state (that may be portrait).
Hence, the only way to go from sensorLandscape
to "disableRotation" is to set explicitly desired orientation. So I have a choice: either call
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
or
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
Obviously, I need to pass a current orientation, but it seems that Android has no function to get it!
getResources().getConfiguration().orientation
returns either Configuration.ORIENTATION_PORTRAIT
or Configuration.ORIENTATION_LANDSCAPE
that is of no use.
getWindowManager().getDefaultDisplay().getRotation()
seems to be more promising. It returns Surface.ROTATION_0
... Surface.ROTATION_270
and I tried to use a logic as follows:
int getCurrentOrintation()
{
final int rot = getWindowManager().getDefaultDisplay().getRotation();
if (Surface.ROTATION_0 == rot || Surface.ROTATION_90 == rot)
{
return ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
}
return ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
}
It works fine on Google Nexus, but returns flipped oreintation on Kindle Fire HD.
So, is it possible in principle to temporarily lock orientation change in Android?