Currently I have a TabHost implemented with 3 tabs each containing a separate activity. My question is how do I switch between tabs from within one of the activities that is located inside the tab host. I've looked everywhere and have been unsuccessful in finding a real answer to this problem.
7 Answers
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.

- 37,609
- 9
- 103
- 153

- 3,024
- 4
- 27
- 31
-
Bear in mind that this technique, reliant upon `ActivityGroup`, is now deprecated. – CommonsWare Aug 17 '11 at 09:57
-
If the technique based on ActivityGroup is deprecated, what is the recommended technique? – Michal Kottman Dec 23 '11 at 12:20
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.

- 391
- 1
- 12
-
+1 - I do the same thing, and it works great. What I have done though is to define some public static ints in the TabActivity to represent different desired states. I pass these constants as data in the Intent and then do a switch on them in onNewIntent...gives me a finer grained control plus the ability to do additional logic if required. – Rich May 13 '10 at 13:09
-
Good point. Don't take my code literally. Also, it's missing a '}'. :) – Anders Petersson May 14 '10 at 19:44
-
By doing it this way isn't there going to be different activities left behind (or more as you do it more) when you intent to other tabs or will android:launchMode="singleTask" force the other tabs to be destroyed or will they still be in memory? – crv Jun 13 '10 at 05:25
-
if you able do that then put the code open source coz me having same problem & stuck coz what is mentioned is not working properly for me ..... – Sumant Mar 17 '11 at 12:47
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.

- 552
- 7
- 24
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.

- 986,068
- 189
- 2,389
- 2,491
-
1This isn't really an answer to my question. At the point of development I am in I really cannot switch out Activities for a view. Funny thing is I was using a View based TabHost originally then the Android Dev site switched there tutorial to Activity. I assumed that this was the correct approach to tabs. – crv Mar 31 '10 at 14:23
-
1"This isn't really an answer to my question." Sure it is. The fact that you don't like the answer does not mean it is not an answer. Another thing you can try is to have the tabbed activity in question call `getParent()`. Reputedly, this will return your outer `TabActivity` (or whatever activity has your `TabHost`). You can then implement some method on that outer activity to affect your tab changes. I have not tried this, and I still do not like the approach, but it might work. – CommonsWare Mar 31 '10 at 14:58
-
I was able to switch tabs using getParent to access the TabHost from within a tab's activity. However, when the tab changes two separate interfaces (the old tab, and the new tab which was switched to) display on top of each other. I've been investigating how to destroy or hide the previous tab and have been unsuccessful. I tried using the finish() method after the tab has been switched but this results in the entire application shutting off. – crv Apr 12 '10 at 04:51
-
2Commons, I've noticed that your answers are often a suggestion to do something completely differently than the OP wants or a suggestion simply not to do what they are trying. Obviously, you have helped a great many people on SO and are very knowledgable, but perhaps you are getting burnt out a bit on SO and aren't answering the question in front of you. Just a thought. – Kenny Wyland Aug 17 '11 at 04:19
-
@Kenny Wyland: The technique the OP requested is now officially deprecated and should not be used. It should never have been used, since it was a waste of RAM, CPU, and battery in the first place. – CommonsWare Aug 17 '11 at 10:02
-
Only 1.3% of Android devices are using Android OS 3.x. Forgive us all if we aren't falling over ourselves to rewrite all of our classes to support an operating system that is practically non-existent. – Kenny Wyland Aug 17 '11 at 18:39
-
1@musselwhizzle: http://developer.android.com/reference/android/app/ActivityGroup.html – CommonsWare Jan 16 '12 at 00:30
I had a slightly different problem and thought I'd add this for anyone else facing a similar situation. I have an activity-based tabbed application and one of the tab activities launches another activity which is not controlled by the tabHost. I needed to have a button on this activity finish() (ie: return back to the main Tab view) and switch to a different tab at the same time.
I decided to handle it with a BroadcastReceiver. In the class that sets up the TabHost, I added this class:
class ChangeTabReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "ChangeTabReceiver: received");
TabHost tabHost = getTabHost();
tabHost.setCurrentTab(1);
}
}
..then defined the vars:
ChangeTabReceiver changeTabReceiver;
IntentFilter changeTabFilter;
..then added to onCreate():
changeTabReceiver = new ChangeTabReceiver();
changeTabFilter = new IntentFilter(myApplication.CHANGE_TAB);
registerReceiver(changeTabReceiver, changeTabFilter);
Finally in the new activity when you want to close that activity and switch the tabs, do this:
Intent intent = new Intent(myApplication.CHANGE_TAB);
this.sendBroadcast(intent);
this.finish();
Of course you could make a method to switch to various tabs by passing the tab index -- but in my case this behavior only occurs in one activity so I decided to keep it simple...

- 4,020
- 2
- 34
- 34
public void switchTab(int index){
MintTrack ParentActivity;
ParentActivity = (MintTrack) this.getParent();
ParentActivity.getTabHost().setCurrentTab(index);
}

- 16,511
- 11
- 49
- 56

- 21
- 1
I just put a public static TabHost tabHost;
in my TabActivity
.
Then from any other tab I can do a MyTabActivity.tabHost.setCurrentTab(tabNumber);
Works fine for me (but I wish I'd used Fragments from the start.. I was just following the Tab tutorial in the Android documentation and working from there)

- 5,767
- 9
- 45
- 60