title: Fragments Behave Weirdly When Using addToBackStack()
body: <p>An SSCCE for this issue is <a href="https://github.com/win93/FragmentTest" rel="nofollow noreferrer">available on GitHub</a>.</p>
  
  <p>For future readers, the original example is on a <a href="https://github.com/win93/FragmentTest/tree/original" rel="nofollow noreferrer">branch of the same project</a>, and the fix is available <a href="https://github.com/win93/FragmentTest/commit/e5f4921d9560559b3785c3c139e7721ec0dd4ea8" rel="nofollow noreferrer">in this diff</a>.</p>
  
  <p>This SSCCE has a ListView and a row of buttons. The buttons are supposed to change the data in the ListView, and the listView rows (when clicked) are supposed to open a new fragment and advance the backstack while staying in the same activity.</p>
  
  <p>If do the following things, it produces the following result:</p>
  
  <ol>
  <li>Open the app.</li>
  <li>Tap the ListView. - <code>FragmentTransaction.replace(...)</code> with <code>addToBackStack(true)</code></li>
  <li>Tap any of the buttons. - <code>FragmentTransaction.replace(...)</code> with <code>addToBackStack(false)</code></li>
  <li>Tap the back button.</li>
  </ol>
  
  <p>Result:</p>
  
  <p><img src="https://i.stack.imgur.com/1RLwR.png" alt="Overlapping Fragments"></p>
  
  <p>Both fragments become visible, but I only want the first loaded fragment (<code>ListTwoFragment</code> in code) to display. Is this how fragments are supposed to work? If so, how can I get the desired effect?</p>
  
  <p>MainActivity.java: </p>
  
  <pre><code>public class MainActivity extends FragmentActivity implements ListTwoFragment.Callbacks,
      ListThreeFragment.Callbacks {
      public static final String KEY_ARGS = "args";
  
      private String curUri = "";
      private String curArgs = "";
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          selectContent(false);
      }
  
      private void selectContent(boolean addToBackStack) {
          Fragment fragment;
  
          if (curUri.isEmpty()) {
              // Use default fragment
              fragment = new ListTwoFragment();
              curUri = ListTwoFragment.class.getName();
          }
          else {
              try {
                  Class&lt;Fragment&gt; fragmentClass = (Class&lt;Fragment&gt;) Class.forName(curUri);
                  fragment = fragmentClass.newInstance();
              }
              catch (Exception e) { // ClassNotFound, IllegalAccess, etc.
                  return;
              }
          }
  
          // Configure fragment
          Bundle args = new Bundle();
          args.putString(KEY_ARGS, curArgs);
          fragment.setArguments(args);
  
          attachFragment(fragment, addToBackStack, curUri + ";" + curArgs, R.id.fragment_container);
      }
  
      protected void attachFragment(Fragment fragment, boolean addToBackStack, String tag, int replaceId) {
          FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
          transaction.replace(replaceId, fragment, tag);
          if (addToBackStack) transaction.addToBackStack(tag);
          transaction.commit();
      }
  
      @Override
      public void onTwoButtonClick(String title) {
          curUri = ListTwoFragment.class.getName();
          curArgs = title;
          selectContent(false);
      }
  
      @Override
      public void onTwoListClick() {
          curUri = ListThreeFragment.class.getName();
          curArgs = "";
          selectContent(true);
      }
  
      @Override
      public void onThreeButtonClick(String title) {
          curUri = ListThreeFragment.class.getName();
          curArgs = title;
          selectContent(false);
      }
  }
  </code></pre>