title: Switching fragments within tab
body: <p>I'm creating an app that uses ActionBarSherlock. The app consists of three tabs, and in each of them, multiple screens are shown consecutively based on user input. I am able to switch Fragments between tabs, but the switching of fragments <em>within</em> tabs gives a problem. I've tried it like this:</p>
  
  <p>In the main class:</p>
  
  <pre><code>SingleStationFragment singleStationFragment = new SingleStationFragment();
  FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
  transaction.replace(android.R.id.content, singleStationFragment);
  transaction.addToBackStack(null);
  transaction.commit();
  </code></pre>
  
  <p>This <em>does</em> replace the first fragment for the second, but when I change tabs, the second fragment is still visible, showing the content of the new tab on top of the content of the old tab. I think somehow I need to detach the second fragment in <code>onTabUnselected</code>, but I have no idea how to get a reference to this Fragment.</p>
  
  <p>Can anyone help me with this?</p>
  
  <p>For clarity, some significant classes:</p>
  
  <p>My main class:</p>
  
  <pre><code>public class TreinVerkeer extends SherlockFragmentActivity {
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setupTabs(savedInstanceState);
  
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          MenuInflater inflater = getSupportMenuInflater();
          inflater.inflate(R.menu.menu, menu);
          return true;
      }
  
      private void setupTabs(Bundle savedInstanceState) {
          ActionBar actionBar = getSupportActionBar();
          actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
  
          Tab tab = actionBar.newTab().setText("STATIONS").setTabListener(new TabListener&lt;StationsFragment&gt;(this, "stations", StationsFragment.class));
          actionBar.addTab(tab);
  
          tab = actionBar.newTab().setText("ROUTE").setTabListener(new TabListener&lt;RouteFragment&gt;(this, "route", RouteFragment.class));
          actionBar.addTab(tab);
  
          tab = actionBar.newTab().setText("DELAYS").setTabListener(new TabListener&lt;DelaysFragment&gt;(this, "delays", DelaysFragment.class));
          actionBar.addTab(tab);
  
          if (savedInstanceState != null) {
              actionBar.setSelectedNavigationItem(savedInstanceState.getInt("tab", 0));
          }
      }
  
      @Override
      protected void onSaveInstanceState(Bundle outState) {
          super.onSaveInstanceState(outState);
          outState.putInt("tab", getSupportActionBar().getSelectedNavigationIndex());
      }
  }
  </code></pre>
  
  <p>The TabListener (from <a href="http://developer.android.com/guide/topics/ui/actionbar.html#Tabs" rel="noreferrer">"Adding Navigations Tabs" on the Android developer site</a> with some minor changes):</p>
  
  <pre><code>public class TabListener&lt;T extends SherlockFragment&gt; implements com.actionbarsherlock.app.ActionBar.TabListener {
      private SherlockFragment mFragment;
      private final Activity mActivity;
      private final String mTag;
      private final Class&lt;T&gt; mClass;
  
      /**
       * Constructor used each time a new tab is created.
       * 
       * @param activity
       *            The host Activity, used to instantiate the fragment
       * @param tag
       *            The identifier tag for the fragment
       * @param clz
       *            The fragment's Class, used to instantiate the fragment
       */
      public TabListener(Activity activity, String tag, Class&lt;T&gt; clz) {
          mActivity = activity;
          mTag = tag;
          mClass = clz;
      }
  
      /* The following are each of the ActionBar.TabListener callbacks */
  
      public void onTabSelected(Tab tab, FragmentTransaction ft) {
      SherlockFragment preInitializedFragment = (SherlockFragment) mActivity.getSupportFragmentManager().findFragmentByTag(mTag);
  
      // Check if the fragment is already initialized
      if (mFragment == null &amp;&amp; preInitializedFragment == null) {
          // If not, instantiate and add it to the activity
          mFragment = (SherlockFragment) SherlockFragment.instantiate(mActivity, mClass.getName());
          ft.add(android.R.id.content, mFragment, mTag);
      } else if (mFragment != null) {
          // If it exists, simply attach it in order to show it
          ft.attach(mFragment);
      } else if (preInitializedFragment != null) {
          ft.attach(preInitializedFragment);
          mFragment = preInitializedFragment;
      }
  }
  
      public void onTabUnselected(Tab tab, FragmentTransaction ft) {
          if (mFragment != null) {
              // Detach the fragment, because another one is being attached
              ft.detach(mFragment);
          }
      }
  
      public void onTabReselected(Tab tab, FragmentTransaction ft) {
          // User selected the already selected tab. Usually do nothing.
      }
  }
  </code></pre>
  
  <p>And <code>StationsFragment</code> (<code>RouteFragment</code> and <code>DelaysFragment</code> are the same, with only different text)</p>
  
  <pre><code>public class StationsFragment extends SherlockFragment {
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
          return inflater.inflate(R.layout.stationsfragment, container, false);
      }
  }
  </code></pre>