Disable and enable orientation changes in activity in Android programmatically

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.

+7
android android-activity rotation screen-orientation android-orientation
source share
4 answers

If you want to disable phone orientation changes, you can use this code in the manifest

 <activity android:name=".class_name" 

// if you want your screen in portrait

 android:screenOrientation="portrait" > 

// if you want you to show in landscape mode

 android:screenOrientation="landscape" > 

and if you want your phone to change orientation, but not be able to restart the process, you can use the onConfigurationChanged method in your class:

 @Override public void onConfigurationChanged(Configuration newConfig) { // ignore orientation change if (newConfig.orientation != Configuration.ORIENTATION_LANDSCAPE) { super.onConfigurationChanged(newConfig); } } 
+11
source share

Try it.

There are no errors in the code. Right, I checked it. Use this code in onConfigurationChanged , which can give a difference, and another thing is used below the code to disable reset activity.

 <activity android:name="YourActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" android:windowSoftInputMode="adjustPan"/> 
+1
source share

You can override the onConfiguratoinChanged method to disable orientation changes:

 @Override public void onConfigurationChanged(Configuration newConfig) { // ignore orientation change if (newConfig.orientation != Configuration.ORIENTATION_LANDSCAPE) { super.onConfigurationChanged(newConfig); } } 

Remember to enable the orientation listener in the AndroidManifest.xml file:

 <activity android:configChanges="orientation" > 
0
source share

Simple: one line to add activity name in android manifest

 android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale 

It worked for me.

0
source share

All Articles