title: Swapping Fragments in a Single Activity?
body: <p>On the tablet we have two fragments (two different views of the same data) that sit next to each other. On mobile devices we'd like to switch between these two fragments at the push of a button. The mobile layout looks something like this:</p>
  
  <pre><code>&lt;RelativeLayout&gt;
    &lt;fragment id="container" name="fragA"/&gt;
    &lt;ImageButton onClick="swapFragments" /&gt;
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>In the activity's <code>swapFragments(View)</code> method, I'm attempting to use the <code>FragmentManager</code> to replace <code>fragA</code> with <code>fragB</code>:</p>
  
  <pre><code>FragmentManager fragmentManager = getSupportFragmentManager();
  FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
  fragmentTransaction.replace(R.id.container, new FragB());
  fragmentTransaction.commit();
  </code></pre>
  
  <p>...but I can always see <code>fragA</code> through the transparent parts of <code>fragB</code>, leading me to believe that it's just putting <code>fragB</code> on top of <code>fragA</code>, not replacing it.</p>
  
  <p>I'm starting down the path of using <code>hide(Fragment)</code> and similar methods in the transaction, but that doesn't look like the right way to do it.</p>
  
  <p>Any tips on how to swap these fragments the right way?</p>
  
  <p><strong>Edit</strong>: I saw the answer to <a href="https://stackoverflow.com/questions/5907673/android-cant-replace-one-fragment-with-another">this question</a>. This confuses me, though, as I need to be able to specify a different layout for tablet and phone. If I have to programmatically add fragments, how do I avoid code specific to each layout in the activity (i.e.</p>
  
  <pre><code> if(TABLET) {
     addFragmentA();
     addFragmentB();
   } else {
     addFragmentA();
   }
  </code></pre>