views:

2033

answers:

4

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.

+1  A: 

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.

CommonsWare
This 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.
GrandPrix
"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
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.
GrandPrix
A: 

hey GrandPrix ,

i am having the exact same problem mentioned by u above

and it seams like you have found the solution

so would you please share it ...

Uhhh, that's kind of the point of this website.
greg7gkb
+2  A: 

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.

Anders Petersson
+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
Good point. Don't take my code literally. Also, it's missing a '}'. :)
Anders Petersson
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?
GrandPrix
+1  A: 

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

GrandPrix