title: Switching views when a tab is clicked in Android
body: <p>I know this question has been asked and I kind of know what I need to do; I just need a small push to get me over the finish line. Here is what I have.</p>
  
  <p>Android app with two tabs and I need to switch fragment view based on which tab is selected. Here is my code so far.</p>
  
  <p>Main Activity</p>
  
  <pre><code>    @Override
  public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
      // This is called when a tab is selected.
      Log.d(app, "Tab: " + tab.getPosition());
  
      switch (tab.getPosition()) {
          case 0:
              Test1Fragment test1Fragment = new Test1Fragment();
              fragmentTransaction.add(R.id.fragment_container, test1Fragment);
              break;
          case 1:
              //Intent i = new Intent(this, Test1.class);
              //startActivity(i);
              Test2Fragment test2Fragment = new Test2Fragment();
              fragmentTransaction.add(R.id.fragment_container, test2Fragment);
              break;
          case 2:
              break;
          default:
      }
  }
  
  // Implemented from ActionBar.TabListener
  @Override
  public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
      // This is called when a previously selected tab is unselected.
  }
  
  // Implemented from ActionBar.TabListener
  @Override
  public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
      // This is called when a previously selected tab is selected again.
  }
  </code></pre>
  
  <p>main_activity.xml</p>
  
  <pre><code>&lt;FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:id="@+id/fragment_container"&gt;
  </code></pre>
  
  <p></p>
  
  <p>I just do not know how to inflate the fragments. This code will show test1Fragment when tab one is clicked but test1Fragment still shows when tab two is clicked. I know I need to remove and replace. I just don't know how to do it. Please help </p>