Here is the situation:
I have a simple Activity which change its content during orientation changes.
I want the ability to force the opposite orientation (landscape -> portrait, portrait -> landscape), in a response to some button click , and still handle orientation changes after that.
I'll try to explain myself with a concrete example (which I wish to accomplish) -
Let's say the device is in portrait mode -> so the activity starts in portrait mode.
Now the user clicks on some button, which in reaction forces the activity to be in landscape mode.
Now the user rotates the device to landscape mode, BUT nothing should happen to the activity, because the activity has been forced to be in landscape mode.
Now the user rotates the device back to portrait mode, and the activity are becoming in portrait mode again.
So, here's how I force orientation:
public void onButtonClick()
{
if (mLastOrientation == Configuration.ORIENTATION_LANDSCAPE)
{
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
}
else
{
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
}
}
And here's the orientation change handling:
@Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
int currentOrientation = newConfig.orientation;
//
// Some UI changes according to orientation .............
//
mLastOrientation = currentOrientation;
// Here's where I'm allowing device orientation changes again
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
}
The problem is that when I'm calling setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED), The activity is changing back to the current device orientation - which results in force orientation to not properly work.
Any idea how to solve this?
thanks in advance!