title: Custom Android TabHost Slow Fragments
body: <p>I use ActionbarSherlock and SlidingMenu. </p>
  
  <p>My MainActivity does not access a DB and no Parsing of what ever. Everything is static.</p>
  
  <p>Tabs are generated dynamicly depending on the "Section" you choose from the SlidingMenu. After 2 click the app becomes awefully slow.</p>
  
  <p>Below is my main view. 
      
      </p>
  
  <pre><code>    &lt;TabHost
          android:id="@android:id/tabhost"
          android:layout_width="match_parent"
          android:layout_height="match_parent" &gt;
  
          &lt;LinearLayout
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical" &gt;
  
              &lt;TabWidget
                  android:id="@android:id/tabs"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:orientation="horizontal" /&gt;
  
              &lt;FrameLayout
                  android:id="@+id/content_frame"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:layout_weight="1"
                  android:background="#0000FF" &gt;
              &lt;/FrameLayout&gt;
  
              &lt;FrameLayout
                  android:id="@android:id/tabcontent"
                  android:layout_width="0dp"
                  android:layout_height="0dp" &gt;
  
              &lt;/FrameLayout&gt;
  
          &lt;/LinearLayout&gt;
      &lt;/TabHost&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>The code used to manage Tabhost &amp; Fragments : </p>
  
  <pre><code>public void selectPage(MenuPageItem page, Page subNavigationPage) {
      if (page == null || subNavigationPage == null
              || (page.equals(selectedPage) &amp;&amp; subNavigationPage.equals(selectedSubNavigation))) {
          return;
      }
      Log.d(TAG, "Show the page: " + page.getTitle() + " / subNavigationPage: " + subNavigationPage.getTitle());
  
      boolean needUpdateTabUI = !(page.equals(selectedPage));
      selectedPage = page;
      selectedSubNavigation = subNavigationPage;
  
      // 1) Manage tabHost
      if (needUpdateTabUI) {
          Log.d(TAG, "need to update tab layout");
          updatePageTabUI();
      }
      // Change selected tab
      mTabHost.setCurrentTabByTag(selectedSubNavigation.getTitle());
  
      // 2) Change Contentfragment
      Log.d(TAG, "update fragment");
      // TODO: check the current fragment is not already the good one and just
      // call its updateData method
      updatePageFragment();
  
      // Close the sliding menu if opened
      if (getSlidingMenu().isShown()) {
          Handler h = new Handler();
  
          h.postDelayed(new Runnable() {
              public void run() {
                  getSlidingMenu().showContent();
              }
          }, 50);
      }
  }
  
  private void updatePageTabUI() {
      if (selectedPage.getPages().size() &gt; 1) {
          // More than one sub navigation -&gt; show the tab host
          // mTabHost.setVisibility(View.VISIBLE);
          mTabHost.setup();
          mTabHost.clearAllTabs();
          int i = 0;
          for (final Page subnav : selectedPage.getPages()) {
              Log.d(TAG, "add tab tag=" + subnav.getTitle() + " title=" + subnav.getTitle());
              mTabHost.addTab(mTabHost.newTabSpec(subnav.getTitle()).setIndicator(subnav.getTitle())
                      .setContent(new EmptyTabFactory()));
              mTabHost.getTabWidget().getChildAt(i).setOnClickListener(new OnClickListener() {
                  @Override
                  public void onClick(View v) {
                      selectPage(selectedPage, subnav);
                  }
              });
              i++;
          }
  
      } else {
          // Only one sub navigation -&gt; hide the tab host
          // mTabHost.setVisibility(View.GONE);
          mTabHost.setup();
          mTabHost.clearAllTabs();
      }
      mTabHost.requestLayout();
      mTabHost.invalidate();
  }
  
  private void updatePageFragment() {
      try {
          PageFragment fragContent = (PageFragment) selectedSubNavigation.getFragmentClass().newInstance();
          fragContent.setSubNavigationPage(selectedSubNavigation);
          FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
          fragmentTransaction.replace(R.id.content_frame, fragContent);
          fragmentTransaction.commit();
      } catch (InstantiationException e) {
          e.printStackTrace();
      } catch (IllegalAccessException e) {
          e.printStackTrace();
      }
  }
  </code></pre>