views:

50

answers:

3

Hi, I have an Android application which has four tabs (I use a main TabActivity with TabHost and TabSpecs). In one of my sub activity (activity opened in a tab), i need to open a tab not by clicking on the tab title and i don't know how to do this. For example, i have a button in my activity and when i click on it, it opens a different tab. For the moment, it is what i do:

Intent intent = new Intent(myActivity.this, myTabActivity.class);
intent.putExtra("ComeFrom", true);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

Then in the TabActivity, if i get true reading the "ComeFrom" extra i open the wished tab but the problem is that it kills all the other activies. So, if someone knows a better (cleaner) way to do that trick, please tell me...

A: 

You have to use TabHost's "setCurrentTab(...)" for that. In one of my projects, I created a static method in the main Activity (the one with the TabHost), named "swtichToTab(int tab)". In my subactivites (those inside the tabs) could then just call "MainActivity.switchToTab()" to trigger switching.

It may not be the cleanest method, I'm sure you can achieve this using broadcast intents too.

Scythe
Thanks; this works ok for my current need ;)
homega52
A: 

You can create a BroadcastReceiver and send a broadcast with the index of the tab as extra

fedj
A: 

You can use views instead of activities for the content of the tabs. This way, the code is simpler and doesn't use as much memory. Plus, you then can use the setCurrentTab(tabIndex) method to easily switch between views.

I have a simple tutorial here. It has a tab activity with a list and map view. When you you click on an item in the list, the activity dynamically goes to the map view (using the setCurrentTab(tabIndex) method). You can easily modify this to have a button switch views.

Josh Clemm