title: Problems with Android Fragment back stack
body: <p>I've got a massive problem with the way the android fragment backstack seems to work and would be most grateful for any help that is offered.</p>
  
  <p>Imagine you have 3 Fragments</p>
  
  <p><code>[1] [2] [3]</code></p>
  
  <p>I want the user to be able to navigate <code>[1] &gt; [2] &gt; [3]</code> but on the way back (pressing back button) <code>[3] &gt; [1]</code>.</p>
  
  <p>As I would have imagined this would be accomplished by not calling <code>addToBackStack(..)</code> when creating the transaction that brings fragment <code>[2]</code> into the fragment holder defined in XML.</p>
  
  <p>The reality of this seems as though that if I dont want <code>[2]</code> to appear again when user presses back button on <code>[3]</code>, I must not call <code>addToBackStack</code> in the transaction that shows fragment <code>[3]</code>. This seems completely counter-intuitive (perhaps coming from the iOS world).</p>
  
  <p>Anyway if i do it this way, when I go from <code>[1] &gt; [2]</code> and press back I arrive back at <code>[1]</code> as expected.</p>
  
  <p>If I go <code>[1] &gt; [2] &gt; [3]</code> and then press back I jump back to <code>[1]</code> (as expected).
  Now the strange behavior happens when I try and jump to <code>[2]</code> again from <code>[1]</code>. First of all <code>[3]</code> is briefly displayed before <code>[2]</code> comes into view. If I press back at this point <code>[3]</code> is displayed, and if I press back once again the app exits.</p>
  
  <p>Can anyone help me to understand whats going on here? </p>
  
  <p><BR>
  And here is the layout xml file for my main activity:
  <BR></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical" &gt;
  
  &lt;fragment
          android:id="@+id/headerFragment"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          class="com.fragment_test.FragmentControls" &gt;
      &lt;!-- Preview: layout=@layout/details --&gt;
  &lt;/fragment&gt;
  &lt;FrameLayout
          android:id="@+id/detailFragment"
          android:layout_width="match_parent"
          android:layout_height="fill_parent"
  
          /&gt;
  </code></pre>
  
  <p></p>
  
  <p><BR>
  <BR></p>
  
  <p><strong>Update</strong>
  This is the code I'm using to build by nav heirarchy</p>
  
  <pre><code>    Fragment frag;
      FragmentTransaction transaction;
  
  
      //Create The first fragment [1], add it to the view, BUT Dont add the transaction to the backstack
      frag = new Fragment1();
  
      transaction = getSupportFragmentManager().beginTransaction();
      transaction.replace(R.id.detailFragment, frag);
      transaction.commit();
  
      //Create the second [2] fragment, add it to the view and add the transaction that replaces the first fragment to the backstack
      frag = new Fragment2();
  
      transaction = getSupportFragmentManager().beginTransaction();
      transaction.replace(R.id.detailFragment, frag);
      transaction.addToBackStack(null);
      transaction.commit();
  
  
      //Create third fragment, Dont add this transaction to the backstack, because we dont want to go back to [2] 
      frag = new Fragment3();
      transaction = getSupportFragmentManager().beginTransaction();
      transaction.replace(R.id.detailFragment, frag);
      transaction.commit();
  
  
       //END OF SETUP CODE-------------------------
      //NOW:
      //Press back once and then issue the following code:
      frag = new Fragment2();
      transaction = getSupportFragmentManager().beginTransaction();
      transaction.replace(R.id.detailFragment, frag);
      transaction.addToBackStack(null);
      transaction.commit();
  
      //Now press back again and you end up at fragment [3] not [1]
  </code></pre>
  
  <p>Many thanks</p>