Code example for TabHost

Methods: addTabnewTabSpecsetOnTabChangedListenersetup

0
    /** 
     * Step 2: Setup TabHost 
     */ 
    private void initialiseTabHost(Bundle args) {
        mTabHost = (TabHost)findViewById(android.R.id.tabhost);
        mTabHost.setup();
        TabInfo tabInfo = null;
        AddGroup.addTab(this, this.mTabHost, this.mTabHost.newTabSpec("Tab1").setIndicator("Class"), ( tabInfo = new TabInfo("Tab1", ClassFragment.class, args)));
        this.mapTabInfo.put(tabInfo.tag, tabInfo);
        AddGroup.addTab(this, this.mTabHost, this.mTabHost.newTabSpec("Tab2").setIndicator("CCA"), ( tabInfo = new TabInfo("Tab2", CCAFragment.class, args)));
        this.mapTabInfo.put(tabInfo.tag, tabInfo);
        AddGroup.addTab(this, this.mTabHost, this.mTabHost.newTabSpec("Tab3").setIndicator("Others"), ( tabInfo = new TabInfo("Tab3", OthersFragment.class, args)));
        this.mapTabInfo.put(tabInfo.tag, tabInfo);
        // Default to first tab 
        this.onTabChanged("Tab1");
        // 
        mTabHost.setOnTabChangedListener(this);
    } 
  
    /** 
     * @param activity 
     * @param tabHost 
     * @param tabSpec 
     * @param clss 
     * @param args 
     */ 
    private static void addTab(AddGroup activity, TabHost tabHost, TabHost.TabSpec tabSpec, TabInfo tabInfo) {
        // Attach a Tab view factory to the spec 
        tabSpec.setContent(activity.new TabFactory(activity));
        String tag = tabSpec.getTag();
  
        // Check to see if we already have a fragment for this tab, probably 
        // from a previously saved state.  If so, deactivate it, because our 
        // initial state is that a tab isn't shown. 
        tabInfo.fragment = activity.getSupportFragmentManager().findFragmentByTag(tag);
        if (tabInfo.fragment != null && !tabInfo.fragment.isDetached()) {
            android.support.v4.app.FragmentTransaction ft = activity.getSupportFragmentManager().beginTransaction();
            ft.detach(tabInfo.fragment);
            ft.commit();
            activity.getSupportFragmentManager().executePendingTransactions();
        } 
  
        tabHost.addTab(tabSpec);
    } 
  
    /** (non-Javadoc) 
     * @see android.widget.TabHost.OnTabChangeListener#onTabChanged(java.lang.String) 
     */