Android - Switch Tabs from within an Activity within a tab

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.

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.


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.


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


public void switchTab(int index){
   MintTrack ParentActivity;
   ParentActivity = (MintTrack) this.getParent();
   ParentActivity.getTabHost().setCurrentTab(index);
}

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)


Need Your Help

Pass form variables to php. Prevent page refresh

javascript php jquery ajax forms

I am working on a web site which displays some data that is retrieved from a database using php. Now, there are also other chekcboxes, which are included in a form. Based on the user input on these

Any grunt packages exist for including html partials from an external web source?

curl gruntjs include

I looked and I don't seem to see any grunt modules that can accommodate a url (http://domain.com/route) when pulling partials.