title: Android Fragment lifecycle over orientation changes
body: <p>Using the compatibility package to target 2.2 using Fragments.</p>
  
  <p>After recoding an activity to use fragments in an app I could not get the orientation changes/state management working so I've created a small test app with a single FragmentActivity and a single Fragment.</p>
  
  <p>The logs from the orientation changes are weird, with multiple calls to the fragments OnCreateView.</p>
  
  <p>I'm obviously missing something - like detatching the fragment and reattaching it rather than creating a new instance, but I can't see any documentation which would indicate where I'm going wrong.</p>
  
  <p>Can anyone shed some light on what I'm doing wrong here please.
  Thanks</p>
  
  <p>The log is as follows after orientation changes.</p>
  
  <pre><code>Initial creation
  12-04 11:57:15.808: D/FragmentTest.FragmentTestActivity(3143): onCreate
  12-04 11:57:15.945: D/FragmentTest.FragmentOne(3143): OnCreateView
  12-04 11:57:16.081: D/FragmentTest.FragmentOne(3143): OnCreateView-&gt;SavedInstanceState null
  
  
  Orientation Change 1
  12-04 11:57:39.031: D/FragmentTest.FragmentOne(3143): onSaveInstanceState
  12-04 11:57:39.031: D/FragmentTest.FragmentTestActivity(3143): onCreate
  12-04 11:57:39.031: D/FragmentTest.FragmentOne(3143): OnCreateView
  12-04 11:57:39.031: D/FragmentTest.FragmentOne(3143): OnCreateView-&gt;SavedInstanceState not null
  12-04 11:57:39.031: D/FragmentTest.FragmentOne(3143): OnCreateView
  12-04 11:57:39.167: D/FragmentTest.FragmentOne(3143): OnCreateView-&gt;SavedInstanceState null
  
  
  Orientation Change 2
  12-04 11:58:32.162: D/FragmentTest.FragmentOne(3143): onSaveInstanceState
  12-04 11:58:32.162: D/FragmentTest.FragmentOne(3143): onSaveInstanceState
  12-04 11:58:32.361: D/FragmentTest.FragmentTestActivity(3143): onCreate
  12-04 11:58:32.361: D/FragmentTest.FragmentOne(3143): OnCreateView
  12-04 11:58:32.361: D/FragmentTest.FragmentOne(3143): OnCreateView-&gt;SavedInstanceState not null
  12-04 11:58:32.361: D/FragmentTest.FragmentOne(3143): OnCreateView
  12-04 11:58:32.361: D/FragmentTest.FragmentOne(3143): OnCreateView-&gt;SavedInstanceState not null
  12-04 11:58:32.498: D/FragmentTest.FragmentOne(3143): OnCreateView
  12-04 11:58:32.498: D/FragmentTest.FragmentOne(3143): OnCreateView-&gt;SavedInstanceState null
  </code></pre>
  
  <p>Main Activity (FragmentActivity)</p>
  
  <pre><code>public class FragmentTestActivity extends FragmentActivity {
  /** Called when the activity is first created. */
  
  private static final String TAG = "FragmentTest.FragmentTestActivity";
  
  
  FragmentManager mFragmentManager;
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
  
      Log.d(TAG, "onCreate");
  
      mFragmentManager = getSupportFragmentManager();
      FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
  
      FragmentOne fragment = new FragmentOne();
  
      fragmentTransaction.add(R.id.fragment_container, fragment);
      fragmentTransaction.commit();
  }
  </code></pre>
  
  <p>And the fragment</p>
  
  <pre><code>public class FragmentOne extends Fragment {
  
  private static final String TAG = "FragmentTest.FragmentOne";
  
  EditText mEditText;
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
          Bundle savedInstanceState) {
  
      Log.d(TAG, "OnCreateView");
  
      View v = inflater.inflate(R.layout.fragmentonelayout, container, false);
  
      // Retrieve the text editor, and restore the last saved state if needed.
      mEditText = (EditText)v.findViewById(R.id.editText1);
  
      if (savedInstanceState != null) {
  
          Log.d(TAG, "OnCreateView-&gt;SavedInstanceState not null");
  
          mEditText.setText(savedInstanceState.getCharSequence("text"));
      }
      else {
          Log.d(TAG,"OnCreateView-&gt;SavedInstanceState null");
      }
      return v;
  }
  
  @Override
  public void onSaveInstanceState(Bundle outState) {
      super.onSaveInstanceState(outState);
  
      Log.d(TAG, "FragmentOne.onSaveInstanceState");
  
      // Remember the current text, to restore if we later restart.
      outState.putCharSequence("text", mEditText.getText());
  }
  </code></pre>
  
  <p>Manifest</p>
  
  <pre><code>&lt;uses-sdk android:minSdkVersion="8" /&gt;
  
  &lt;application
      android:icon="@drawable/ic_launcher"
      android:label="@string/app_name" &gt;
      &lt;activity
          android:label="@string/app_name"
          android:name=".activities.FragmentTestActivity" 
          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;
  </code></pre>