Search code examples
androidandroid-activityandroid-tabhost

I want my Tabs to appear in every activity but it only appears in the activity where the tabs are assigned


When in Home tab, there is a List View of menus that you can click. And if you click one, it will go to a new activity but the tab is gone.I want tab to appear in this new activity and other activities as well.

Here is my Main activity(onCreate)

    @Override
protected void onCreate(Bundle savedInstanceState) {


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

    hostTab = (TabHost) findViewById(R.id.tabhost);
    hostTab.setup(this.getLocalActivityManager());

    //setting tabs(images)
    mIntent = new Intent(this, HomeActivity.class);
    specTab = hostTab.newTabSpec("home").setIndicator(" ",getResources().getDrawable(R.drawable.home_tab)).setContent(mIntent);
    hostTab.addTab(specTab);
    hostTab.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.home_tab);
    //hostTab.getTabWidget().getChildAt(0).setLayoutParams(new LinearLayout.LayoutParams(150,50));

    mIntent = new Intent(this, ContactUsActivity.class);
    specTab = hostTab.newTabSpec("contactUs").setIndicator(" ",getResources().getDrawable(R.drawable.contact_us_tab)).setContent(mIntent);
    hostTab.addTab(specTab);
    hostTab.getTabWidget().getChildAt(1).setBackgroundResource(R.drawable.contact_us_tab);

    mIntent = new Intent(this, TravelDetailsActivity.class);
    specTab = hostTab.newTabSpec("travelDetails").setIndicator(" ",getResources().getDrawable(R.drawable.travel_details_tab)).setContent(mIntent);
    hostTab.addTab(specTab);
    hostTab.getTabWidget().getChildAt(2).setBackgroundResource(R.drawable.travel_details_tab);

    mIntent = new Intent(this, PortGuide.class);
    specTab = hostTab.newTabSpec("portGuide").setIndicator(" ",getResources().getDrawable(R.drawable.port_guide_tab)).setContent(mIntent);
    hostTab.addTab(specTab);
    hostTab.getTabWidget().getChildAt(3).setBackgroundResource(R.drawable.port_guide_tab);

    mIntent = new Intent(this, MoreMenu.class);
    specTab = hostTab.newTabSpec("more").setIndicator(" ",getResources().getDrawable(R.drawable.more_tab)).setContent(mIntent);
    hostTab.addTab(specTab);
    hostTab.getTabWidget().getChildAt(4).setBackgroundResource(R.drawable.more_tab);

}

Here is my main activity xml

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"

    android:id="@+id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"  >
    <LinearLayout 
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:padding="5dp">
       <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="wrap_content"
            android:layout_height="0dp" 
            android:layout_weight="1">
        </FrameLayout>
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" 
            android:gravity="bottom">
        </TabWidget>
     </LinearLayout>
</TabHost>

Please feel free to comment what I'm missing to post for you to further understand my problem. Thanks in advance.


Solution

  • You should create a FragmentActivity to host multiple Fragments as children. You will be able to keep the tabs with a FragmentTabHost.

    See:
    FragmentTabHost Documentation
    TabHost Tutorial
    Creating a Tab Layout with FragmentTabHost and Fragments

    Hope will be helpful.