Android - switch tabs from inside a tab action

Currently, I have TabHost implemented with 3 tabs, each of which contains a separate action. My question is how to switch between tabs from one of the actions located inside the tab node. I searched everywhere and could not find a real answer to this problem.

+42
android android-intent tabs
Mar 29 '10 at 22:52
source share
7 answers

After a long time struggling with this problem, I was able to find a solution for switching tabs when using activity-based tabs.

In the parent activity class where the tab is created, I implemented a method similar to the one below:

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

Inside the tab that I would like to switch inside to another tab, I created a method below:

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

If you need a good example of this code, you can see my MintTrack project here and here .

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

Activity tabs are great because they can be split into your own xml file. Events can also be organized into your own Java file instead of being cluttered into one. At the same time, some of the things that, in your opinion, will be easily complicated with activity-based tabs. It is difficult to transfer information between tabs without creating overhead. Activity bookmarks also use more memory / processor time, since they have Activity overheads around each of them. Please consider this and many other trade-offs before diving into using TabHost based TabHost . Now I know that personally I would look based on TabHost if I used them again.

+92
Jun 13 '10 at 5:17
source share

I ran into the same problem. Although one action for all tabs would be better, sometimes accepting an easy exit is a rational choice.

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

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

Send the intent with the required tab:

 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); } } 

There are drawbacks to this solution, but I do not understand the details. Someone may know enough to point to them.

+10
May 13, '10 at 13:01
source share

First, I set up a method for my main class that extends TabActivity, calling it "MainActivity"

 public TabHost getMyTabHost() { return tabHost; } 

Then I add the tab activity class;

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

It worked for me.

+6
Aug 21 '11 at 19:20
source share

Step # 1: replace tab-holding actions with canvas tabs using the best setContent() form on TabSpec

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

I still do not see any benefit from having the Activity contents of the tab, rather than a simple View . The presence of Activity , since the contents of the tab takes up processor time and memory (and therefore battery life), and makes things like you, trying to make it a lot harder.

+5
Mar 29 '10 at 23:36
source share

I had a slightly different problem, and I thought that I would add this for everyone who is faced with a similar situation. I have an activity-based tabbed application, and one kind of tabs launches another action that is not controlled by tabHost. I needed to press a button on this completion of the activity () (i.e., return to the main view of the tab) and at the same time switch to another tab.

I decided to process it using BroadcastReceiver. In the class that sets up 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 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 action, when you want to close this action and switch tabs, do the following:

 Intent intent = new Intent(myApplication.CHANGE_TAB); this.sendBroadcast(intent); this.finish(); 

Of course, you can make a way to switch to different tabs by passing the tab index, but in my case this behavior happens in only one action, so I decided to keep it simple ...

+3
Jul 09 '11 at 10:21
source share
 public void switchTab(int index){ MintTrack ParentActivity; ParentActivity = (MintTrack) this.getParent(); ParentActivity.getTabHost().setCurrentTab(index); } 
+2
Jun 20 '11 at 16:21
source share

I just put public static TabHost tabHost; in my TabActivity .

Then from any other tab I can do MyTabActivity.tabHost.setCurrentTab(tabNumber);

Works fine for me (but I'm sorry I didn't use Snippets from the start. I just followed the Tab tutorial in the Android documentation and worked from there)

+2
Mar 02 2018-12-12T00:
source share



All Articles