title: onCreate() and onCreateView() invokes a lot more than required (Fragments)
body: <p>Can somebody explain why the <code>onCreate()</code> and <code>onCreateView()</code> are being invoked so many times which increments with each orientation change?</p>
  
  <p>Here is very simple app which consists of one <code>Activity</code> composed of two <code>Fragments</code>. The second <code>Fragment</code> loads <strong>dynamically</strong>. If you define these two <code>Fragments</code> in <code>main.xml</code> there would not be such a behavior.</p>
  
  <p>Here is <code>main.xml</code>:
      </p>
  
  <pre><code>       &lt;fragment class="ets.saeref.Left"
          android:id="@+id/left_frag"
          android:layout_weight="70"
          android:layout_width="match_parent"
          android:layout_height="match_parent"/&gt;
  
       &lt;FrameLayout android:id="@+id/right_frag" 
          android:layout_weight="30"
          android:layout_width="match_parent" 
          android:layout_height="match_parent"/&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>Here is left frag:</p>
  
  <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical" android:layout_width="match_parent"
      android:layout_height="match_parent" android:background="#000000"&gt;
      &lt;Button android:text="Landscape" android:id="@+id/button1"
          android:layout_width="wrap_content" android:layout_height="wrap_content"&gt;&lt;/Button&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>Here is right frag:</p>
  
  <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical" android:layout_width="match_parent"
      android:layout_height="match_parent" android:background="#ffffff"&gt;
      &lt;Button android:text="Landscape" android:id="@+id/button1"
          android:layout_width="wrap_content" android:layout_height="wrap_content"&gt;&lt;/Button&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>Left.class:</p>
  
  <pre><code>public class Left extends Fragment {
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          Log.i("Left", "onCreate()");
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
              Bundle savedInstanceState) {
          Log.i("Left", "onCreateView()");
          return inflater.inflate(R.layout.left, container, false);
      }
  }
  </code></pre>
  
  <p>Right.class:</p>
  
  <pre><code>public class Right extends Fragment {
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          Log.i("Right", "onCreate()");
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
              Bundle savedInstanceState) {
          Log.i("Right", "onCreateView()");
          return inflater.inflate(R.layout.right, container, false);
      }
  }
  </code></pre>
  
  <p>Main class:</p>
  
  <pre><code>public class Main extends Activity {
      /** Called when the activity is first created. */
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
  
          Fragment fg = new Right();
          getFragmentManager().beginTransaction().add(R.id.right_frag, fg)
              .commit();
          Log.i("Main", "onCreate()");
      }
  }
  </code></pre>
  
  <p>Log after several orientation changes:</p>
  
  <pre><code>08-28 21:47:38.220: INFO/Main(1099): onCreate()
  08-28 21:47:38.220: INFO/Right(1099): onCreateView()
  08-28 21:47:38.220: INFO/Right(1099): onCreateView()
  08-28 21:47:38.220: INFO/Right(1099): onCreateView()
  08-28 21:47:38.220: INFO/Right(1099): onCreate()
  08-28 21:47:38.220: INFO/Right(1099): onCreateView()
  08-28 21:47:41.110: INFO/ActivityManager(142): Config changed: {1.0 0mcc0mnc en_US sw800dp w1280dp h752dp xlrg land finger -keyb/v/h -nav/h s.162}
  08-28 21:47:41.140: INFO/Right(1099): onCreate()
  08-28 21:47:41.140: INFO/Right(1099): onCreate()
  08-28 21:47:41.140: INFO/Right(1099): onCreate()
  08-28 21:47:41.140: INFO/Right(1099): onCreate()
  08-28 21:47:41.170: INFO/Left(1099): onCreate()
  08-28 21:47:41.170: INFO/Left(1099): onCreateView()
  08-28 21:47:41.170: INFO/Main(1099): onCreate()
  08-28 21:47:41.170: INFO/Right(1099): onCreateView()
  08-28 21:47:41.170: INFO/Right(1099): onCreateView()
  08-28 21:47:41.170: INFO/Right(1099): onCreateView()
  08-28 21:47:41.170: INFO/Right(1099): onCreateView()
  08-28 21:47:41.190: INFO/Right(1099): onCreate()
  08-28 21:47:41.190: INFO/Right(1099): onCreateView()
  08-28 21:47:45.070: INFO/ActivityManager(142): Config changed: {1.0 0mcc0mnc en_US sw800dp w800dp h1232dp xlrg port finger -keyb/v/h -nav/h s.163}
  08-28 21:47:45.120: INFO/Right(1099): onCreate()
  08-28 21:47:45.120: INFO/Right(1099): onCreate()
  08-28 21:47:45.120: INFO/Right(1099): onCreate()
  08-28 21:47:45.120: INFO/Right(1099): onCreate()
  08-28 21:47:45.120: INFO/Right(1099): onCreate()
  08-28 21:47:45.130: INFO/Left(1099): onCreate()
  08-28 21:47:45.130: INFO/Left(1099): onCreateView()
  08-28 21:47:45.130: INFO/Main(1099): onCreate()
  08-28 21:47:45.130: INFO/Right(1099): onCreateView()
  08-28 21:47:45.130: INFO/Right(1099): onCreateView()
  08-28 21:47:45.130: INFO/Right(1099): onCreateView()
  08-28 21:47:45.140: INFO/Right(1099): onCreateView()
  08-28 21:47:45.140: INFO/Right(1099): onCreateView()
  08-28 21:47:45.140: INFO/Right(1099): onCreate()
  08-28 21:47:45.140: INFO/Right(1099): onCreateView()
  </code></pre>