Android : How to have multiple activities under a single tab of TabActivity

Yes, its possible.
1. Lets first have our main activity as
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. Activity1  Activity2
Activity1 tab launches Activity1
Activity2 tab launches Activity2
 
Now tricky part is how to launch Activity3 from Activity1 so that Activity3 also shows the tabs.
 
3. To achieve this we extend Activity1 to be derived from ActivityGroup. Clicking on a button in activity1 will launch Activity3 as subactivity of Activity1.
 
public class Activity1 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.activity1);
launchButton = (Button)findViewById(R.id.Button01);
launchButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
Intent activity3Intent = new Intent(v.getContext(), Activity3.class);
StringBuffer urlString = new StringBuffer();
//Activity1 parentActivity = (Activity1)getParent();
replaceContentView("activity3", activity3Intent);
}
});
 
public void replaceContentView(String id, Intent newIntent) {
View view = getLocalActivityManager().startActivity(id,newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)) .getDecorView(); this.setContentView(view);
}        }
 
4. We can even launch another activity, Activity4 from Activity3 and still have the tabs at the bottom.
public class Activity3 extends Activity {
Button launchButton;
/** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity3);
launchButton = (Button)findViewById(R.id.Button01);
launchButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {               Intent activity4Intent = new Intent(v.getContext(), Activity4.class);
StringBuffer urlString = new StringBuffer();
Activity1 parentActivity = (Activity1)getParent();
parentActivity.replaceContentView("activity4", activity4Intent);
} }); }}
 
Enjoy!!

You can find the previously running activity and launch that

For example: In your Activity3. You can have:
 
this.m_backButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) {
Intent intent = new Intent(v.getContext(), Activity1.class); Activity1 parentActivity = (Activity1)getParent(); parentActivity.replaceContentView("activity1", intent, Intent.FLAG_ACTIVITY_REORDER_TO_FRONT ); }
});
 
I will also take out Activity1 from ActivityGroup.
So have each Activity under its own class such as Activity1 extends Activity.
Use ActivityGroup to manage them such as:
public class ActivityGroup1 extends ActivityGroup
{
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); replaceContentView("activity1", new Intent(this, Activity1.class), Intent.FLAG_ACTIVITY_CLEAR_TOP); }
 
public void replaceContentView(String id, Intent newIntent, int flag) { View view = getLocalActivityManager().startActivity(id, newIntent.addFlags(flag)).getDecorView(); this.setContentView(view); }
}
 
 

Partners

Pioneer-Electronics-Logo

 

Texas-Instruments-logo

 

Zypr

GammaPoint Tweets

Navigation