Save the date! Android Dev Summit is coming to Mountain View, CA on November 7-8, 2018.
added in version 22.1.0
belongs to Maven artifact com.android.support:support-fragment:28.0.0-alpha1

FragmentTabHost

public class FragmentTabHost
extends TabHost implements TabHost.OnTabChangeListener

java.lang.Object
   ↳ android.view.View
     ↳ android.view.ViewGroup
       ↳ android.widget.FrameLayout
         ↳ android.widget.TabHost
           ↳ android.support.v4.app.FragmentTabHost


Special TabHost that allows the use of Fragment objects for its tab content. When placing this in a view hierarchy, after inflating the hierarchy you must call setup(Context, FragmentManager, int) to complete the initialization of the tab host.

Here is a simple example of using a FragmentTabHost in an Activity:

import com.example.android.supportv4.R;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;

/**
 * This demonstrates how you can implement switching between the tabs of a
 * TabHost through fragments, using FragmentTabHost.
 */
public class FragmentTabs extends FragmentActivity {
    private FragmentTabHost mTabHost;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.fragment_tabs);
        mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
        mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);

        mTabHost.addTab(mTabHost.newTabSpec("simple").setIndicator("Simple"),
                FragmentStackSupport.CountingFragment.class, null);
        mTabHost.addTab(mTabHost.newTabSpec("contacts").setIndicator("Contacts"),
                LoaderCursorSupport.CursorLoaderListFragment.class, null);
        mTabHost.addTab(mTabHost.newTabSpec("custom").setIndicator("Custom"),
                LoaderCustomSupport.AppListFragment.class, null);
        mTabHost.addTab(mTabHost.newTabSpec("throttle").setIndicator("Throttle"),
                LoaderThrottleSupport.ThrottledLoaderListFragment.class, null);
    }
}

This can also be used inside of a fragment through fragment nesting:

import com.example.android.supportv4.R;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTabHost;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class FragmentTabsFragmentSupport extends Fragment {
    private FragmentTabHost mTabHost;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        mTabHost = new FragmentTabHost(getActivity());
        mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.fragment1);

        mTabHost.addTab(mTabHost.newTabSpec("simple").setIndicator("Simple"),
                FragmentStackSupport.CountingFragment.class, null);
        mTabHost.addTab(mTabHost.newTabSpec("contacts").setIndicator("Contacts"),
                LoaderCursorSupport.CursorLoaderListFragment.class, null);
        mTabHost.addTab(mTabHost.newTabSpec("custom").setIndicator("Custom"),
                LoaderCustomSupport.AppListFragment.class, null);
        mTabHost.addTab(mTabHost.newTabSpec("throttle").setIndicator("Throttle"),
                LoaderThrottleSupport.ThrottledLoaderListFragment.class, null);

        return mTabHost;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mTabHost = null;
    }
}

Summary

Inherited constants

From class android.view.ViewGroup
From class android.view.View

Inherited fields

From class android.view.View

Public constructors

FragmentTabHost(Context context)
FragmentTabHost(Context context, AttributeSet attrs)

Public methods

void addTab(TabHost.TabSpec tabSpec, Class<?> clss, Bundle args)
void onTabChanged(String tabId)
void setOnTabChangedListener(TabHost.OnTabChangeListener l)
void setup()

This method was deprecated in API level 26.1.0. Don't call the original TabHost setup, you must instead call setup(Context, FragmentManager) or setup(Context, FragmentManager, int).

void setup(Context context, FragmentManager manager, int containerId)
void setup(Context context, FragmentManager manager)

Protected methods

void onAttachedToWindow()
void onDetachedFromWindow()
void onRestoreInstanceState(Parcelable state)
Parcelable onSaveInstanceState()

Inherited methods

From class android.widget.TabHost
From class android.widget.FrameLayout
From class android.view.ViewGroup
From class android.view.View
From class java.lang.Object
From interface android.view.ViewTreeObserver.OnTouchModeChangeListener
From interface android.view.ViewParent
From interface android.view.ViewManager
From interface android.graphics.drawable.Drawable.Callback
From interface android.view.KeyEvent.Callback
From interface android.view.accessibility.AccessibilityEventSource
From interface android.widget.TabHost.OnTabChangeListener

Public constructors

FragmentTabHost

added in version 22.1.0
FragmentTabHost (Context context)

Parameters
context Context

FragmentTabHost

added in version 22.1.0
FragmentTabHost (Context context, 
                AttributeSet attrs)

Parameters
context Context

attrs AttributeSet

Public methods

addTab

added in version 22.1.0
void addTab (TabHost.TabSpec tabSpec, 
                Class<?> clss, 
                Bundle args)

Parameters
tabSpec TabHost.TabSpec

clss Class

args Bundle

onTabChanged

added in version 22.1.0
void onTabChanged (String tabId)

Parameters
tabId String

setOnTabChangedListener

void setOnTabChangedListener (TabHost.OnTabChangeListener l)

Parameters
l TabHost.OnTabChangeListener

setup

added in version 26.1.0
void setup ()

This method was deprecated in API level 26.1.0.
Don't call the original TabHost setup, you must instead call setup(Context, FragmentManager) or setup(Context, FragmentManager, int).

setup

added in version 22.1.0
void setup (Context context, 
                FragmentManager manager, 
                int containerId)

Parameters
context Context

manager FragmentManager

containerId int

setup

added in version 22.1.0
void setup (Context context, 
                FragmentManager manager)

Parameters
context Context

manager FragmentManager

Protected methods

onAttachedToWindow

void onAttachedToWindow ()

onDetachedFromWindow

void onDetachedFromWindow ()

onRestoreInstanceState

void onRestoreInstanceState (Parcelable state)

Parameters
state Parcelable

onSaveInstanceState

Parcelable onSaveInstanceState ()

Returns
Parcelable