2hamed
2hamed

Reputation: 9067

How to change tabs in Android?

I have three tabs that each has its own Activity. The tabs are as follows:

Home [HomeActivity]
Search [SearchActivity]
Account [AccountActivity]

I have a Main Activity which handles the main TabHost object and this is its content:

public TabHost tabHost;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tabHost = getTabHost();      

tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("Home").setContent(new Intent(this, HomeActivity.class)));

tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("Search").setContent(new Intent(this, SearchActivity.class).putExtra("callX", true)));
tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("Account").setContent(new Intent(this, AccountActivity.class)));
tabHost.setCurrentTab(0);
}

Now I have a button in Search tab which I need when it is clicked, no matter what, the Home tab should activate. I guess I should somehow call the setCurrentTab() method on tabHost object but I don't how to access it inside the SearchActivity class?
I probably should use Intent for that which I have no idea how to use.

Upvotes: 0

Views: 228

Answers (1)

Akram
Akram

Reputation: 7527

set a method to my main class, which extends TabActivity let's call it "MainActivity"

public TabHost getMyTabHost() { 
return tabHost; 
}

Then add my tab activity class;

MainActivity ta = (MainActivity) this.getParent();
TabHost th = ta.getMyTabHost();
th.setCurrentTab(0);

or follow a better aproach at this

Upvotes: 1

Related Questions