Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent starting the activity at the first tab in a TabActivity?

Tags:

android

I have a TabActivity, which contains 4 activities. My code sets the second tab as the current tab:

public class MyTabActivity extends TabActivity {
    TabHost tabHost = getTabHost();
    TabHost.TabSpec spec; // Resusable TabSpec for each tab
    Intent intent; // Reusable Intent for each tab

    TextView tabView;

    // Create an Intent to launch an Activity for the tab (to be reused)
    intent = new Intent().setClass(this, Activity1.class);

    spec = tabHost.newTabSpec("Tab 1");
    spec.setContent(intent);
    tabView = (TextView) inflater.inflate(R.layout.ff_tab_indicator, null);
    tabView.setText("Tab 1");
    spec.setIndicator(tabView);
    tabHost.addTab(spec);

    intent = new Intent().setClass(this, Activity2.class);
    spec = tabHost.newTabSpec("Tab 2");
    spec.setContent(intent);
    tabView = (TextView) inflater.inflate(R.layout.ff_tab_indicator, null);
    tabView.setText("Tab 2");
    spec.setIndicator(tabView);
    tabHost.addTab(spec);

    intent = new Intent().setClass(this, Activity3.class);
    spec = tabHost.newTabSpec("Tab 3");
    spec.setContent(intent);
    tabView = (TextView) inflater.inflate(R.layout.ff_tab_indicator, null);
    tabView.setText("Tab 3");
    spec.setIndicator(tabView);
    tabHost.addTab(spec);

    intent = new Intent().setClass(this, Activity4.class);
    spec = tabHost.newTabSpec("Tab 4");
    spec.setContent(intent);
    tabView = (TextView) inflater.inflate(R.layout.ff_tab_indicator, null);
    tabView.setText("Tab 4");
    spec.setIndicator(tabView);
    tabHost.addTab(spec);

    tabHost.setCurrentTab(1);
}

The problem is, when the MyTabActivity starts, it starts both activity in the first tab and the activity in the second tab. I just want it to start the activity in the second tab, since it is set to be the current tab. What should I do?

Thanks.

like image 611
user256239 Avatar asked Jun 25 '10 21:06

user256239


2 Answers

Try this:

tabHost.setCurrentTab(0);   
like image 64
user790291 Avatar answered Nov 15 '22 06:11

user790291


What about just reordering the Tabs so that the default becomes the first?

like image 21
Heiko Rupp Avatar answered Nov 15 '22 07:11

Heiko Rupp