title: Assigning Fragments to tabs in the ActionBar with different orientations
body: <p>I have 3 fragments and an activity.  I want to enable tabs on the <code>ActionBar</code> and assign a <code>Fragment</code> to each of the 3 tabs.  How do I hook that up correctly?</p>
  
  <p><strong>ORIGINAL POST</strong></p>
  
  <p>I have an app that I'm developing using the Google I/O app as a guide.  I've implemented tabs into the <code>ActionBar</code>.  They seem to be working until the tablet's orientation changes.  For example, all 3 tabs have a <code>Fragment</code>.  I can switch between them just fine, but when I change the orientation, whatever the <code>Fragment</code> I was last viewing stays visible, but clicking the tabs no longer changes the view... like they have become disconnected.  As expected, going back to the original orientation does not "fix" it.</p>
  
  <p>I've looked into saving and restoring state, but I'm not seeing how those would help.  </p>
  
  <p><strong>EDIT</strong> </p>
  
  <p>module level:</p>
  
  <pre><code>Fragment mFragmentA = (Fragment) new AFragmentTab();
  Fragment mFragmentB = (Fragment) new BFragmentTab();
  Fragment mFragmentC = (Fragment) new CFragmentTab();
  </code></pre>
  
  <p>I have something like this in the activity's <code>onCreate</code>:</p>
  
  <pre><code>ActionBar actionBar = getActionBar();
  actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
  actionBar.setDisplayShowTitleEnabled(false);
  
  ActionBar.Tab tabA = actionBar.newTab().setText("text a");
  ActionBar.Tab tabB = actionBar.newTab().setText("text b");
  ActionBar.Tab tabC = actionBar.newTab().setText("text c");
  
  tabA.setTabListener(this);
  tabB.setTabListener(this);
  tabC.setTabListener(this);
  
  actionBar.addTab(tabA);
  actionBar.addTab(tabB);
  actionBar.addTab(tabC);
  </code></pre>
  
  <p>and a <code>TabListener</code> like this: </p>
  
  <p><strong><em>EDIT</strong> this is removed</em></p>
  
  <pre><code>class MyTabListener implements ActionBar.TabListener {
      private Fragment mFragment;
  
      // Called to create an instance of the listener when adding a new tab
      public MyTabListener(Fragment fragment) {
          mFragment = fragment;
      }
  
      public void onTabSelected(Tab tab, FragmentTransaction ft) {
          ft.add(R.id.fragment_content, mFragment, null);
      }
  
      public void onTabUnselected(Tab tab, FragmentTransaction ft) {
          ft.remove(mFragment);
      }
  
      public void onTabReselected(Tab tab, FragmentTransaction ft) {
          // do nothing
      }
  }
  </code></pre>
  
  <p><strong>EDIT</strong></p>
  
  <p>I've moved the <code>TabListener</code>.  Instead of it being a separate class, I implement the <code>TabListener</code> on the <code>Activity</code>.  Then on the <code>Selected</code> and <code>Unselected</code> methods I have something like:</p>
  
  <pre><code>public void onTabSelected(Tab tab, FragmentTransaction ft) {
      switch (tab.getPosition()) {
      case 0:
          ft.add(R.id.fragment_content, mFragmentA, null);
          break;
      case 1:
          ft.add(R.id.fragment_content, mFragmentB, null);
          break;
      case 2:
          ft.add(R.id.fragment_content, mFragmentC, null);
          break;
      }
  }
  
  public void onTabUnselected(Tab tab, FragmentTransaction ft) {
      switch (tab.getPosition()) {
      case 0:
          ft.remove(mFragmentA);
          break;
      case 1:
          ft.remove(mFragmentB);
          break;
      case 2:
          ft.remove(mFragmentC);
          break;
      }
  }
  </code></pre>
  
  <p>It's still doing the same thing.  I really don't know what's happening.  </p>