title: Why does onLayout and onSizeChanged get called twice on an orientation change?
body: <p>I've noticed that onLayout and onSizeChanged get called <em>twice</em> in immediate succession after an orientation change, either from landscape->portrait or from portrait->landscape, when handling the configuration change from an Activity.  Furthermore, the first onLayout/onSizeChanged contain the <em>old</em> dimensions (before the rotate), while the 2nd onLayout/onSizeChanged contain the <em>new</em> (correct) dimensions.</p>
  
  <p>Does anyone know why, and/or how to work around this?  It seems like perhaps the screen size change happens quite some time after the configuration change - i.e., the dimensions are not correct immediately after the configuration change when <code>onConfigurationChanged</code> is called?</p>
  
  <p>Here's the debug output of the code below, showing both onLayout/onSizeChanged calls after a rotation from Portrait to Landscape (note that the device is 540x960, so the landscape width should be 960 while the portrait width is 540):</p>
  
  <pre><code>03-13 17:36:21.140: DEBUG/RotateTest(27765): onConfigurationChanged: LANDSCAPE
  03-13 17:36:21.169: DEBUG/RotateTest(27765): onSizeChanged:540,884,0,0
  03-13 17:36:21.189: DEBUG/RotateTest(27765): onLayout:true-0,0,540,884
  03-13 17:36:21.239: DEBUG/RotateTest(27765): onSizeChanged:960,464,540,884
  03-13 17:36:21.259: DEBUG/RotateTest(27765): onLayout:true-0,0,960,464
  </code></pre>
  
  <p>Note also that the first onSizeChanged oldwidth and oldheight are 0, indicating that we were just added to the view hierarchy - yet with the <em>wrong</em> dimensions for landscape!</p>
  
  <p>And here is the code that illustrates this behavior:</p>
  
  <h3>MyActivity.java</h3>
  
  <pre><code>package com.example;
  
  import android.app.Activity;
  import android.content.res.Configuration;
  import android.os.Bundle;
  import android.util.Log;
  import android.widget.FrameLayout;
  
  public class MyActivity extends Activity
  {
      private static String TAG = "RotateTest";
  
      @Override
      public void onConfigurationChanged(Configuration newConfig) {
          Log.d(TAG, "onConfigurationChanged: " + (newConfig.orientation == 1 ? "PORTRAIT" : "LANDSCAPE"));
          super.onConfigurationChanged(newConfig);
          _setView();
      }
  
      @Override
      public void onCreate(Bundle savedInstanceState)
      {
          Log.d(TAG, "onCreate");
          super.onCreate(savedInstanceState);
          _setView();
      }
  
      private void _setView() {
          MyHorizontalScrollView scrollView = new MyHorizontalScrollView(this, null);
          setContentView(scrollView);
      }
  }
  </code></pre>
  
  <h3>MyHorizontalScrollView.java</h3>
  
  <pre><code>package com.example;
  
  import android.content.Context;
  import android.util.AttributeSet;
  import android.util.Log;
  import android.widget.HorizontalScrollView;
  
  public class MyHorizontalScrollView extends HorizontalScrollView {
  
      private static String TAG = "RotateTest";
  
      public MyHorizontalScrollView(Context context, AttributeSet attrs) {
          super(context, attrs);
      }
  
      @Override
      protected void onLayout(boolean changed, int l, int t, int r, int b) {
          super.onLayout(changed, l, t, r, b);
          Log.d(TAG, "onLayout:" + String.format("%s-%d,%d,%d,%d", changed, l, t, r, b));
      }
  
      @Override
      protected void onSizeChanged(int w, int h, int oldw, int oldh) {
          super.onSizeChanged(w, h, oldw, oldh);
          Log.d(TAG, "onSizeChanged:" + String.format("%d,%d,%d,%d", w, h, oldw, oldh));
      }
  }
  </code></pre>
  
  <h3>AndroidManifest.xml</h3>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example"
        android:versionCode="1"
        android:versionName="1.0"
          &gt;
  
      &lt;uses-sdk android:minSdkVersion="8" android:targetSdkVersion="9"/&gt;
  
      &lt;application android:label="@string/app_name"
              &gt;
          &lt;activity android:name="MyActivity"
                    android:label="@string/app_name"
                    android:configChanges="orientation"
                  &gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.MAIN" /&gt;
                  &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
      &lt;/application&gt;
  &lt;/manifest&gt; 
  </code></pre>