title: Is this the right way to clean-up Fragment back stack when leaving a deeply nested stack?
body: <p>I'm using the Android Compatibility library to implement fragments and have extended the layout sample so that a fragment contains a button which fires off another fragment.</p>
  
  <p>In the selection pane on the left I have 5 selectable items - <code>A B C D E</code>.</p>
  
  <p>Each loads up a fragment (via <code>FragmentTransaction:replace</code>) in the details pane - <code>a b c d e</code></p>
  
  <p>Now I've extended fragment <code>e</code> to contain a button which loads up another fragment <code>e1</code> also in the details pane. I've done this on fragment <code>e</code>'s onClick method as follows:</p>
  
  <pre><code>FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
  ft.replace(R.id.details_frag, newFrag);
  ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
  ft.addToBackStack(null);
  ft.commit();
  </code></pre>
  
  <p>If I make the following selections:</p>
  
  <pre><code>E - e - e1 - D - E
  </code></pre>
  
  <p>Then fragment <code>e</code> is in the details pane. This is fine and what I want. However, if I hit the <code>back</code> button at this point it does nothing. I have to click it twice because <code>e1</code> is still on the stack. Furthermore after clicking around I got a null pointer exception in onCreateView:</p>
  
  <p>To 'solve' this problem I added the following whenever <code>A B C D E</code> is selected:</p>
  
  <pre><code>FragmentManager fm = getActivity().getSupportFragmentManager();
  for(int i = 0; i &lt; fm.getBackStackEntryCount(); ++i) {
      fm.popBackStack();
  }
  </code></pre>
  
  <p>Just wondering whether this is the correct solution or whether I should be doing something different?</p>