Kevin Langley Jr.
Kevin Langley Jr.

Reputation: 113

TabHost without TabActivity, ActivityGroup, or LocalActivityManager

I am creating an app that has three tabs on the main screen. Each of the tabs I have the content set as separate Activities. The reason I used activities instead of Fragments is because inside one of the tabs I need to be able to have Fragments and nested Fragments are not supported.

At first, I was extending TabActivity to do so, but then I realized that it is deprecated so then I started looking around. I found someone suggested using a ActivityGroup here, but... that too is deprecated.

It seems all the solutions on StackExchange to the problem I am having, contain deprecated functionality. Such as, Android: TabHost without TabActivity , and Android - Tabhost working in Activity class

What is the correct way to have a TabHost and allow the tabs to contain Fragments without using deprecated functionality?

Below is what I currently have

import android.app.ActivityGroup;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

public class HomeTabs extends ActivityGroup {

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        TabHost host = (TabHost)findViewById(android.R.id.tabhost);
        LocalActivityManager mlam = new LocalActivityManager(this, false);
        mlam.dispatchCreate(savedInstanceState);
        host.setup(mlam);

        TabSpec spec = host.newTabSpec("Home");
        spec.setContent(new Intent(this, HomeActivity.class));
        spec.setIndicator("Home");
        host.addTab(spec);

        spec = host.newTabSpec("Browse");
        spec.setContent(new Intent(this, BrowseActivity.class));
        spec.setIndicator("Browse");
        host.addTab(spec);

        spec = host.newTabSpec("About");
        spec.setContent(new Intent(this, AboutActivity.class));
        spec.setIndicator("About");
        host.addTab(spec);
    }
}

Upvotes: 1

Views: 5619

Answers (1)

CommonsWare
CommonsWare

Reputation: 1007533

What is the correct way to have a TabHost and allow the tabs to contain Fragments without using deprecated functionality?

Put views in the tabs, whether statically in the layout as immediate children of the FrameLayout inside the TabHost, or dynamically via the variant of setContent() that takes a TabContentFactory. Those views can, AFAIK, take things that manage fragments (e.g., ViewPager), or possibly fragments directly themselves via <fragment> elements.

Or, don't use TabHost. Use tabs in the action bar, using ActionBarSherlock for backwards compatibility. Or, use a ViewPager. Or, use a ViewFlipper and a TabWidget (which, despite the docs, does not appear to have an actual dependency on TabHost). Or, use a ViewFlipper and some other control mechanism to switch between "tabs".

Or, get rid of your "fragment within the tab content".

Upvotes: 3

Related Questions