且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何启动与特定标签的活动?

更新时间:2023-01-25 17:38:16

您需要将自己与 setCurrentTab 处理它的新的活动的构造。

在呼吁,应该把更多的价值在意向 -

 意向书我=新的意图(这一点,MyTabActivity.class);
i.putExtra(FirstTab,4);
 

而在MyTabActivity的构造 -

 意图I = getIntent();
INT tabToOpen = i.getIntExtra(FirstTab,-1);
如果(tabToOpen!=  -  1){
    //打开正确的选项卡
}
 

I've gone through many examples, questions and tutorials but I've never seen an activity launch (launch a new intent) with a specific tab. I know that one can use .setCurrentTab to switch to a tab, but this can be done only from inside the parent activity tab. How about launching a specific tab contained in one activity from a different activity? Is it possible? If so, then how?

In my code, on a standard activity launch user is shown the first tab, but I want him to go to the fourth tab in case he is being redirected from another activity. My TabHost code (MyTabActivity):

int tabIndex = 0;

          mTabHost.addTab(mTabHost.newTabSpec("top10").setIndicator("Top 10").setContent(R.id.Top_10));
          mTabHost.addTab(mTabHost.newTabSpec("billable").setIndicator("Billable").setContent(R.id.Billable));
          mTabHost.addTab(mTabHost.newTabSpec("product").setIndicator("Product").setContent(R.id.Product));
          mTabHost.addTab(mTabHost.newTabSpec("regular").setIndicator("Regular").setContent(R.id.General));


          mTabHost.setCurrentTab(tabIndex);

Now in another activity:

public void gotoTab() {
//This will take me to the first tab
Intent i = new Intent(this, MyTabActivity.class);
startActivity(i);
finish();
//How to I make it take me to the fourth tab?
}

You will need to handle it yourself with setCurrentTab in the new activity's constructor.

While calling, you should put additional values in the intent -

Intent i = new Intent(this, MyTabActivity.class);
i.putExtra("FirstTab", 4);

And in constructor of MyTabActivity -

Intent i = getIntent();
int tabToOpen = i.getIntExtra("FirstTab", -1);
if (tabToOpen!=-1) {
    // Open the right tab
}