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!!

Stackoverflow error

Hi,
I'm also getting this StackOverflow error. Any ideas on how to solve this? I have created three activities and I have made it possible to circulate between them.
Let me explain my setup a little further. I have three activities, wher B is my ActivityGroup.
A - B - C
Starting at activity B:
When pressing my "Left-button" I get this behavior: A-C-B-A-C-B-A....
When pressing my "Right-button" I get this behavior: C-A-B-C-A-B...
After a few presses I get the StackOverFlowError. :-/

4th activity forse fully close

activity4 button click event i call 5th activity so it forse fully close

qustion

how to set 4th activity parent activity

source code

can you zip and add source code

Problem

Hi,

this code works fine. But I have one problem. I have 4 tabs, to every tab is assigned one activity. Only from one tab I launch another new activity according to this guideline. Before I launch second activity from this tab, I can switch between all activities without any problems. But when I launch new activity, I can't switch to another tab, because the application crashes. Can someone help me solve this problem? Thanks.

GREAT................................... MAN.

From few days i was searching for it. Its very useful for me thanks.....................

Thank you. It was really

Thank you. It was really useful for me

sample application

Hi all,

Can anybody have sample code. and how to use menus, and dialogs in this.

Please do the needful.

Sourcecode

Please add the sourcecode as .zip file. I try that example but they doesnt work on my simulator.

Sourcecode

Please add the sourcecode as .zip file. I try that example but they doesnt work on my simulator.

Thanks

Hey,

Thanks for the code. I'm searching this for past 2 months :) . Thanks you very much.

sourcecode

Please add again the sourcecode as zip.file. I need it too. Thanks

Problem with back button on emulator

Dear admin,
You have given a great work on Tabs. I have worked on this, when i move from Activity1 to Activity3 it is woring fine but i am unable to come back even by using the BACK button on emulator. Are there any possible solutions please give me reply at gvenugopal141@gmail.com

thanks
venu

main tab navigation is not working

Hi,

The article is good and it is working fine, but one problem is there
if you have multiple tabs, First you click on Tab1 then under Tab1 we have Activity page on this we have one button when we click this we will display Activity 2 page, now when i click Tab2 then again Click Tab1 then it is not displaying my main page, it is display Activity 2, it should display one main page, so please guide, how to solve this problem.

Please help me very urgent

Thanking you

Error

Hi
When i create a ListActivity and launch it from ActivityGroup as subactivity i have a problem wich ListView.

I use SimpleAdapter and when i start program the activity is show, i can see it, i can click on it, but when i try to scroll (drag finger only, if i use dragball its works fine ) the app it's crash

if i create normal activity on tab it's works fine.

HashMap item = null;

item = new HashMap();
item.put(ITEM_KEY_NAME, getResources().getText(R.string.label_info_menu_news).toString());
list.add(item);

item = new HashMap();
item.put(ITEM_KEY_NAME, getResources().getText(R.string.label_info_menu_stockmarket).toString());
list.add(item);

item = new HashMap();
item.put(ITEM_KEY_NAME, getResources().getText(R.string.label_info_menu_currency).toString());
list.add(item);

this.adapter = new SimpleAdapter( getApplicationContext(), list, R.layout.info_menu_row, new String[] { ITEM_KEY_NAME }, new int[] { R.id.list_name });

setListAdapter(this.adapter);

Error

Sorry i dont wrote about android version, this error apeers only on 1.5 on 1.6 and grater its works fine.

Animations

Hi, great technique to use the tabs. But when you start a sub activity you don't have the usual "sliding" animation between the views. Is there a way to add this behavior easily ? I don't really know if we have to add a viewFlipper, or if we must use startAnimation().

Thanks

Did you found how to add an

Did you found how to add an animation ?

What if you want to return to Activity1 from Activity3 ?

Hi, I used your technique of subactivities in my app, and it works mostly well. But in one case, I encounter a StackOverFlow error.

In fact, in my case, I have to manage the return between activities. My activity4 can go back to Activity3, and Activity3 can go back to Activity1.
I want to have only one instance of Activity1 and Activity3 launched, and always the same, because Activity3 has a listView that I want to memorize the position.

So the problem is that I succeeded in going back to the Activity3 from Activity4 (I just didn't set any flag on the startActivity method), but I don't know how to go back to Activity1. At the moment, I just use startActivity on Activity1, but I think that it always launches a new instance of Activity1.
So when I tried to do severals roundtrip between Activity1 and Activity3, i get the StackOverFlow error.

The solution is probably simple, but I'd be glad if you could help me, thanks.

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); }
}
 
 

Some Curious Doubts

Hi,

I am just telling you the work flow of my project.

I have one
Home Page ->
on button click ->
tab activity launches Activity1 as default(I have made it Group Activity) ->
from here we can move to Activity2 and Activity3 (both are extending Activity and we can come back from both the activity to Activity1) Activity2 and Activity3 also have some buttons to move on other activities.

But my problem is that when we are making many moves within activity1, 2 and 3.
it is crashing down and showing the Stackoverflow exception.

I have made set the flag as FLAG_ACTIVITY_REORDER_TO_FRONT in all.

As i understands this flag bring the activity to front that is executed before or if it is not in stack it just launches it as new.

I am not getting whenever to use FLAG_ACTIVITY_CLEAR_TOP.

Am i doing right.....

please help me....

Stackoverflow exception.

Hi
Did you get any solution for the Stackoverflow exception that occurs if you have multiple activities. I am also facing the same problem. ANy workaround?

thanks
lucky

Switching between activities like 3 to 4 times gives error.

Hi,

I used the same logic in my app and if I switch between multiple activities like 3 to 4 times on the 4th time i get error Sorry...application has stopped unexpectedly. Please try again" . If you have any idea for the same kindly let me know..

Thanks,
Sree

Stackoverflow exception.

Have you fixed this exception?

Problems

Hi admin,

As i am not getting the meaning of these lines. Plz clearify me..

"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:"

I am not getting the point if you are taking out Activity1 from ActivityGroup then how can you call

"Activity1 parentActivity = (Activity1)getParent(); parentActivity.replaceContentView("activity1", intent, Intent.FLAG_ACTIVITY_REORDER_TO_FRONT );"

As replaceContentView() is a method of ActivityGruop1 class.

Plz help me...

Thanks in advance....

Thanks !

Ok, great solution, it works perfectly.

However, I still have a problem with all this technique. I have a lot of activities to launch in my tabs, and because they are accessible from differents activities, I have to memorize the parents activities all the time, by giving them the parents in the intents.

It's really heavy when I have a succession of 5 or 6 screens, I have to memorize the grand grand parent of the activity, to be able to manage all the returns between activities.

I would really appreciate if I could call the last Activity started by the localActivityManager, just like normal behavior when we are outside tabs.

jump onto previous activity from current activity

i want to know is there any way for come back on previous activity by pressing back button on the simulator?
if yes then how please help me....

Didn't find

Hi, I'd like to have this bahavior too. For now, I have a graphical button in the top left corner of my app, which call the replaceView on the parentActivity as we said before.

I thought that doing the same code here would work

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
// my code ...
return true;
}
return super.onKeyDown(keyCode, event);
}

But it doesn't... If you find a way to do that, let me know.

Dispatch the key event !

So I had a graphical button to go back to the previous activity, but I didn't know why my Android physical back button wasn't working. It was a stupid problem, the event was captured by the activityGroup, and not the current activity that was just the content view of the acivity group.

So we have just to add this in the activityGroup code :

public boolean onKeyDown(int keyCode, KeyEvent event){
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
mView.dispatchKeyEvent(event);
return true;
}
return super.onKeyDown(keyCode, event);
}

with in the replaceContentView :

mView = mLocalActivityManager.startActivity(id, newIntent.addFlags(flag)).getDecorView();

The Activities in the tab can

The Activities in the tab can be switched in the following manner.

First Let us understand the flow:

1. We have in a Tab host , activity (say a list) from which we need to go to the next Activity (say details for the clicked item) under the same tab. For this we can use the concept of replacing the activity.Also setting the flags for the tab selected and other for knowing that details are being shown now

2. When we press back we should get the previous activity under the same tab.For this instead of again replacing the activity we can refresh the tab while using the particular flag for tab which was selected. Also if flag for show details is true we'll go the the list in the same tab or else we will go the activity before the tabwidget (normal use of onBackPressed)

The code can be as follows..

1. For going from list to details...

(This can be in the onClickListener)

private OnClickListener textListener = new OnClickListener() {

@Override
public void onClick(View v) {
Constants.SHOW_DETAILS = true;
Intent intent = new Intent(context, DetailsActivity.class);
replaceContentView("activity3", intent);
}
};

public void replaceContentView(String id, Intent newIntent) {
View view = ((ActivityGroup) context)
.getLocalActivityManager()
.startActivity(id,
newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
.getDecorView();
((Activity) context).setContentView(view);

}

2. When back pressed is done we override on BackPressed in each of the Activity under the tab to go to the list again from the details screen

@Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
if (MathHelper.SHOW_DETAILS) {
Log.e("back", "pressed accepted");
Constants.LIST_ACTIVITY = 1;
Constants.SHOW_DETAILS = false;
Intent intent = new Intent(this, Tab_widget.class);
startActivity(intent);
finish();
}
}

The most important part here is
Constants.LIST_ACTIVITY = 1; it indicates which tab we are in. so the corresponding activities will have its value as 0,1,2...etc

Again to load the correct list (Activty) when the tab activity is refreshed we have to include this in the TabWidget onCreate after the creation of the tabs

tabHost.setCurrentTab(Constants.LIST_ACTIVITY);

Partners

Pioneer Electronics
 
Zypr
 

GammaPoint Tweets

Navigation