Android - Switch Tabs from within an Activity within a tab

After a long time of battling with this problem I've been able to find a solution to switching tabs when using activity based tabs.

In the parent activity class where the tabhost is created I implemented a method like the one below:

public void switchTab(int tab){
            tabHost.setCurrentTab(tab);
}

Inside of the tab that I would like to be able to switch internally to another tab I created the method below:

public void switchTabInActivity(int indexTabToSwitchTo){
            MintTrack parentActivity;
            parentActivity = (MintTrack) this.getParent();
            parentActivity.switchTab(indexTabToSwitchTo);
}

If you would like a good example of this code, you can take a look at my MintTrack project here and here.

As a side note, please be very careful when deciding whether to use view or activity based TabHost.

Activity based tabs are great because they can be separated into there own XML file. Activities can also be organized into there own Java file instead of being cluttered into one. That being said some of the things you would think would be easy become complicated with activity based tabs. Its hard to pass information between tabs without creating overhead. Activity based tabs also use more memory/CPU time as they have the overhead of the Activity around each of them. Please consider this and the many more trade offs before diving into using an Activity based TabHost. I know now that I would personally go with a view based TabHost if I ever used them again.


I encountered the same problem. While a single activity for all tabs would be better, sometimes taking the easy way out is the rational choice.

To avoid creating a new tab activity when a tab wants to change to another tab, I put this in my AndroidManifest.xml:

<activity android:name=".MyTabsActivity"
        android:label="Tabs!"
        android:launchMode="singleTask">

Send an intent with the tab you want:

class OneTabContentActivity {
  void switchTab() {
    final Intent intent = new Intent(mContext, MyTabsActivity.class);
    intent.setAction("Switch to tab 1, please");
    mContext.startActivity(intent);
}

class MyTabsActivity {
  @Override
  protected void onNewIntent (Intent intent) {
    super.onNewIntent(intent);
    getTabHost().setCurrentTab(1);
  }
}

This solution has drawbacks but I'm not clear over the details. Someone else might know enough to point them out.


First, I set a method to my main class, which extends TabActivity let's call it "MainActivity"

public TabHost getMyTabHost() { return tabHost; }

Then, I add my tab activity class;

MainActivity ta = (MainActivity) this.getParent();
TabHost th = ta.getMyTabHost();
th.setCurrentTab(0);

It worked for me.


Step #1: Replace the tabs-holding-activities with tabs-holding-views by using a better form of setContent() on TabSpec

Step #2: Call setCurrentTab() on your TabHost from within your single Activity

I have yet to see any benefit to having an Activity be the content of a tab rather than a simple View. Having an Activity as the content of the tab wastes CPU time and memory (and, hence, battery life) and makes things like you're trying to do much more difficult.