title: Know when rotation is finished using setRequestedOrientation
body: <p>I am trying to write an .apk to measure how long it takes to rotate the screen of an Android device (looping through a couple rotations).</p>
  
  <p>I am using calls to <code>setRequestedOrientation()</code> to reoriente the screen in the 4 positions (<code>PORTRAIT</code>, <code>LANDSCAPE</code>, <code>REVERSE_PORTRAIT</code> and <code>REVERSE_LANDSCAPE</code>).</p>
  
  <p>My problem is I am not able to tell when the rotation is finished. I have tried a few solutions, but each have their problems.</p>
  
  <ol>
  <li>I tried to use <code>onConfigurationChanged()</code>. The problem is that this method gets triggered when the orientation change happens from the sensor, and not from <code>setRequestedOrientation()</code>. I actually have to call <code>setRequestedOrientation()</code> with the <code>FULL_SENSOR</code> parameter for <code>onConfigurationChanged</code> to work after my previous call to <code>setRequestedOrientation()</code>.</li>
  <li>I have tried making <code>setRequestedOrientation()</code> sort of "blocking", by adding a while loop to check the screen orientation (in degrees). I called <code>android.view.Display.getRotation()</code> before <code>setRequestedOrientation()</code> and block with a while loop until it has changed. Unfortunately, it doesn't work either. It returns before the UI gets redisplayed.</li>
  <li>I have tried to follow the life cycle of the activity, by waiting for <code>onCreate()</code> to get called again, to assume the rotation was done. Previously, I was looping until my counter reached a value, in each iteration calling <code>setRequestedOrientation()</code>. In this scenario, my while loop becomes an if statement, and I assume <code>onCreate</code> will call the function each time, but <code>onCreate()</code> doesn't get called after a couple of iterations. The UI doesn't seem to be redrawn as well.</li>
  <li>A final solution that I have found online (but haven't fully been able to implement) is to use the <code>waitForIdleSync()</code> method right after <code>setRequestedOrientation()</code>. <code>waitForIdleSync()</code> is part of the Instrumentation class, and I think this is just made for testing and not for a standard .apk.</li>
  </ol>
  
  <p>Any ideas would be really welcome. I would really appreciate your help.</p>