Update tabs from a tab's activity in a Android TabHost
Asked Answered
C

4

9

I have a TabActivity and am going to have 3 or 4 tabs.

In TabA, I would like to have an ExpandableListView, which will list products. I would like TabC to act as a sort of "shopping cart", probably with a ListView of itself.

Is there a way to interact with the TabHost from the Activity? I would like to be able to click a button inside of TabA and have it update and switch to TabC. Is this possible? Can I get the Tab's activities call its parents methods?

Calefaction answered 31/5, 2010 at 18:14 Comment(0)
S
16

If you implement your tabs as views, rather than individual activities, this would be far simpler, run faster, take up less memory, and be a better solution IMHO.

That being said, in the Tab A Activity, call getParent() to get at the TabActivity. Then, you can use the methods pointed out by disretrospect.

Semester answered 31/5, 2010 at 19:44 Comment(3)
+1 for the getParent() tip - although I do not understand the recommendation to use views instead of activities. I prefer activities to seperate the life cycles and also to seperate the code belonging to each tab.Adam
@K. Claszen: "to seperate the life cycles" -- their lifecycles aren't separate. "also to seperate the code belonging to each tab" -- code separation in the form of Java class files has been around for 15 years. You do not need to use separate activities for this.Semester
Thanks for the response. As I observe, the activities lifecycle methods (onCreate, etc.) are only called if the associated tab is selected. And I like to be able to extend provided activities like ExpandableListActivity in a tab. However, both possibilities are provided and make sence depending on the requirement so I just wondered if there could be a general recommendation.Adam
C
3

Try to use this code to change activity within a tab:

TabActivity parent = (TabActivity) getParent();
TabHost tabhost = parent.getTabHost();
tabhost.setCurrentTab(0);
Cryptograph answered 17/7, 2012 at 1:6 Comment(0)
E
1

You can call getTabHost from a TabActivity, see public TabHost getTabHost().

See Tabhost for setCurrentTab, public void setCurrentTab(int index).

Eadie answered 31/5, 2010 at 19:42 Comment(0)
L
1
  1. In your Activity, use getParent() to get the TabActivity parent
  2. In your TabActivity class, use getLocalActivityManager().getActivity(tabId) to get the Activity object
  3. call the Activity object's method
Loosestrife answered 28/5, 2012 at 9:42 Comment(1)
how about getting the view in that Activity #3Atalie

© 2022 - 2024 — McMap. All rights reserved.