When you’re using a TabHost each tab has it’s own Activity. Assume, you want to change the Activity that means you want to go TabHost one Activity to another new Activity . If you just go on and start new Activity and display it under previous tabhost activity then your Tab Layout is no longer visible.
For that reason you need a ActivityGroup within the Tab where you want to change the Activity.
An ActivityGroup is a screen that contains and runs multiple embedded activities
1. Lets first have our main activity that extend TabActivity
public class multiple extends TabActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_tab);
Resources res = getResources(); // Resource object to get Drawables
TabHost tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab // Create an Intent
// to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, Activity1.class);
spec = tabHost
.newTabSpec(“Activity1”)
.setIndicator(
new CustomTabView(this,
R.drawable.options_selected, “Activity1”))
.setContent(intent);
tabHost.addTab(spec); // Do the same for the other tabs
intent = new Intent().setClass(this, Activity2.class);
spec = tabHost
.newTabSpec(“Activity2”)
.setIndicator(
new CustomTabView(this,
R.drawable.options_selected, “Activity2”))
.setContent(intent);
tabHost.addTab(spec);
}
}
2. Here we create 2 tabs First and Second and two activity fisrtTabActivity secondTabActivity
First tab launches fisrtTabActivity
Second tab launches secondTabActivity
Now tricky part is how to launch thirdActivity from fisrtTabActivity so that thirdActivity also shows in First tabs.
3. To achieve this we extend fisrtTabActivity to be derived from ActivityGroup. Clicking on a button in fisrtTabActivity will launch ThirdActivity as subactivity of fisrtTabActivity.
public class fisrtTabActivity extends ActivityGroup {
protected static LocalActivityManager mLocalActivityManager;
Button launchButton;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
launchButton = (Button) findViewById(R.id.button2);
launchButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent ThirdActivityIntent = new Intent(v.getContext(),
ThirdActivity.class);
StringBuffer urlString = new StringBuffer();
replaceContentView(“ThirdActivity”, ThirdActivityIntent);
}
});
}
public void replaceContentView(String id, Intent newIntent) {
View view = getLocalActivityManager().startActivity(id,
newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
.getDecorView();
this.setContentView(view);
}
}
Happy Coding..