I have an application that creates some background staff. When the background work starts, a circular motion is displayed, if the device rotates during this time, then the action is "reset", and I want to avoid this.
For this reason, I decided to turn off orientation during this process. I saw different topics for this question, but none of them has the right solution, at least in my case.
The posted solutions relate to fixing the orientation of the activity, but you have to deal with the fact that the REVERSE orientations do not return if you use:
getResources().getConfiguration().orientation
The function above returns SCREEN_ORIENTATION_PORTRAIT for both PORTRAIT and REVERSE_PORTRAIT cases (at least in my tests).
So at the end, I used the Rotation value to handle this, so my code is “turn off rotation”:
int rotation = getWindowManager().getDefaultDisplay().getRotation(); switch(rotation) { case Surface.ROTATION_180: setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT); break; case Surface.ROTATION_270: setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE); break; case Surface.ROTATION_0: setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); break; case Surface.ROTATION_90: setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); break; }
And again enable orientation:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
This works fine on a device with Android 4.1.2, but on a device with Android 4.2.1 it does not work as expected.
I think that managing rotation in the active activity cycle should be a common problem, but I could not find a suitable solution. Maybe I was looking for the wrong direction, so any help is really appreciated.
Thanks in advance, Ivan.
android android-activity rotation screen-orientation android-orientation
Ivan BASART
source share