title: Disable and enable orientation changes in an activity in Android programatically
body: <p>I have an App that make some background staff.  When the background work is running a progress Circle is shown, if the device is rotated during this time then the Activity is "reset" and I want to avoid that.</p>
  
  <p>For this reason I decided disable orientation during this process. I have seen differends threads for this question but none with a valid solution, at least in my case. </p>
  
  <p>The solutions posted are about fixing the activity orientation but you have to deal with the fact that the REVERSE orientations are not returned if you use:</p>
  
  <pre><code>getResources().getConfiguration().orientation
  </code></pre>
  
  <p>The function above returns SCREEN_ORIENTATION_PORTRAIT both for PORTRAIT and REVERSE_PORTRAIT cases (at least in my tests).</p>
  
  <p>So at the end I used the Rotation value to deal with that, so my code "disable the rotation" is: </p>
  
  <pre><code>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;
      }
  </code></pre>
  
  <p>And to allow again the orientation:</p>
  
  <pre><code>setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
  </code></pre>
  
  <p>That works perfectly in a device with Android 4.1.2 but in a Device with Android 4.2.1 it is not working as expected.</p>
  
  <p>I guess managing rotation in the activity live cycle should be a common issue but I have not been able to find a suitable solution. May be I'm looking to the wrong direction so any help is really welcomed.</p>
  
  <p>Thanks in advance,
  Ivan.</p>