How to prevent starting the activity at the first tab in a TabActivity?
I have a TabActivity, which contains 4 activities. My code sets the second tab as the current tab:
public class MyTabActivity extends TabActivity {
开发者_JAVA技巧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.
Try this:
tabHost.setCurrentTab(0);
What about just reordering the Tabs so that the default becomes the first?
精彩评论