0

Well as i say i have a tabView on my program and i want to pass some data through the activities. When i try startActivity the tabs disappear. So i want instead of trying startActivity. I Want to change the tab.

I have 3 activities one for each tab and one separate to hold the tabview.

public class Start extends TabActivity {
TabHost tabHost;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.start);

Resources res = getResources(); // Resource object to get Drawables
tabHost = getTabHost();  // The activity TabHost
TabHost.TabSpec spec;  // Reusable TabSpec for each tab
Intent intent;  // Reusable Intent for each tab

intent = new Intent().setClass(this, Home.class);
spec = tabHost.newTabSpec("Home").setIndicator("Home",
                  res.getDrawable(R.drawable.ic_tab_main))
              .setContent(intent);
tabHost.addTab(spec);

// Do the same for the other tabs
intent = new Intent().setClass(this, History.class);
spec = tabHost.newTabSpec("History").setIndicator("History",
                  res.getDrawable(R.drawable.ic_tab_history))
              .setContent(intent);
tabHost.addTab(spec);

intent = new Intent().setClass(this, Settings.class);
spec = tabHost.newTabSpec("Settings").setIndicator("Settings",
                  res.getDrawable(R.drawable.ic_tab_settings))
              .setContent(intent);
tabHost.addTab(spec);

tabHost.setCurrentTab(0);

thats how i add the tabs and i dont know how to change the tab from an other activity. I tried this:

  Start tab;
  tab.tabHost.setCurrentTab(0);

but i got error.... :/

PeeHaa
  • 71,436
  • 58
  • 190
  • 262
Alex Fragotsis
  • 1,248
  • 18
  • 36
  • I don't really get what you want to do.Can you please explain a little more? – Android-Droid Oct 14 '11 at 10:03
  • see: http://stackoverflow.com/questions/1306689/launching-activities-within-a-tab-in-android – Dyonisos Oct 14 '11 at 10:04
  • I have total 4 activities. 1 holds the TabView (Start.java) and another 3 activities one for each tab. i want to pass some data between the activities but when i try startActivity i got the activity on screen but all the tabs are disappear. So instead of startactivity i want to just change the tab so the tas wont disappear and i dont know how to change the tab programmaticaly – Alex Fragotsis Oct 14 '11 at 10:06

2 Answers2

0

You don't need to call startActivity to switch between tabs, the TabHost takes care of that for you. As for passing data between the tabs, have you considered storing the data in the tabActivity? You can get a reference to it from your tabs (the childrens) by using getParent() and casting it to your class, in your case (Start) Like this:

Start parentActivity;
parentActivity = (Start) getParent();

Now you can access data and methods you create in the tabActivity.

Lars
  • 4,082
  • 2
  • 20
  • 20