title: Android - Multiple fragments in ONE tab
body: <p>I searched a lot in the web about the possibility to have multiple fragments in one action bar tab.</p>
  
  <p><a href="https://stackoverflow.com/questions/9216108/actionbar-tabs-with-multiple-fragments">This question</a> comes closest to my needs but the code is not working.</p>
  
  <p>Is there another possibility to include multiple fragments in one tab?</p>
  
  <hr>
  
  <p>This is the <code>StartActivity</code></p>
  
  <pre><code>public class StartActivity extends FragmentActivity implements ActionBar.TabListener {
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_start);
  
          // Set up the action bar.
          final ActionBar actionBar = getActionBar();
          actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
  
          actionBar.addTab(
                actionBar.newTab()
               .setText("Karte")
                .setTabListener(new MapFragmentListener(this)));
  
      }
  </code></pre>
  
  <p>The corresponding layout <code>acticity_start</code> contains two frame layouts for the two fragments that will be placed in the tab.</p>
  
  <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:orientation="vertical" &gt;  
  
      &lt;FrameLayout
          android:id="@+id/fragment_map"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"/&gt;
      &lt;FrameLayout
          android:id="@+id/fragment_realtime"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"/&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>The <code>TabListener</code> looks like this:</p>
  
  <pre><code>private class TabListener implements ActionBar.TabListener {
  
          private static final String fragment1_tag = "fragment1_tag";
          private static final String fragment2_tag = "fragment2_tag";
  
          private FragmentActivity activity;
          private RealtimeFragment fragment1;
          private RealtimeFragment fragment2;
  
          public TabListener(FragmentActivity activity) {
  
              this.activity = activity;
  
              android.support.v4.app.FragmentTransaction ft = activity.getSupportFragmentManager().beginTransaction();
  
              fragment1 = (RealtimeFragment) activity.getSupportFragmentManager().findFragmentByTag(fragment1_tag);
              if (fragment1 != null &amp;&amp; !fragment1.isDetached()) {
                  ft.detach(fragment1);
              }
  
              fragment2 = (RealtimeFragment) activity.getSupportFragmentManager().findFragmentByTag(fragment2_tag);
              if (fragment2 != null &amp;&amp; !fragment2.isDetached()) {
                  ft.detach(fragment2);
              }
  
              ft.commit();
          }
  
          @Override
          public void onTabReselected(Tab tab, FragmentTransaction ft) {
              // TODO Auto-generated method stub
  
          }
  
          @Override
          public void onTabSelected(Tab tab, FragmentTransaction ft) {
  
              android.support.v4.app.FragmentTransaction fragmentTransaction = activity.getSupportFragmentManager().beginTransaction();
  
              if(fragment1 == null) {
                  fragment1 = new RealtimeFragment();
                  fragmentTransaction.add(R.id.fragment_map, fragment1, fragment1_tag);
              } else {
                  fragmentTransaction.attach(fragment1);
              }
  
  
              if(fragment2 == null) {
                  fragment2 = new RealtimeFragment();
                  fragmentTransaction.add(R.id.fragment_realtime, fragment2, fragment2_tag);
              } else {
                  fragmentTransaction.attach(fragment2);
              }
  
  
              fragmentTransaction.commit();
  
          }
  
          @Override
          public void onTabUnselected(Tab tab, FragmentTransaction ft) {
  
              android.support.v4.app.FragmentTransaction fragementTransaction = activity.getSupportFragmentManager().beginTransaction();
  
              if(fragment1_tag != null)
                  fragementTransaction.detach(fragment1);
              if(fragment2 != null)
                  fragementTransaction.detach(fragment2);
  
  
              fragementTransaction.commit();
  
          }
  
      }
  </code></pre>
  
  <p>The class <code>RealtimeFragment</code> and the corresponding layout <code>fragment_realtime</code> looks like this:</p>
  
  <pre><code>public class RealtimeFragment extends Fragment {
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
              Bundle savedInstanceState) {
           View view = inflater.inflate(R.layout.fragment_realtime, container, false);
          return view;
      }
  }
  
  
  &lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical" &gt;
  
      &lt;Button
          android:id="@+id/button1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Button" /&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>In one tab now two fragments of the class <code>RealtimeFragment</code> should be displayed. The fragments just show one button but NOTHING is displayed! Why?</p>