import java.util.ArrayList;
import android.app.Activity;
import android.app.ActivityGroup;
import android.app.LocalActivityManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Window;
/**
* The purpose of this Activity is to manage the activities in a tab.
* Note: Child Activities can handle Key Presses before they are seen here.
* @author Eric Harlow
*/
public class TabGroupActivity extends ActivityGroup {
private ArrayList<String> mIdList;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (mIdList == null) mIdList = new ArrayList<String>();
}
/**
* This is called when a child activity of this one calls its finish method.
* This implementation calls {@link LocalActivityManager#destroyActivity} on the child activity
* and starts the previous activity.
* If the last child activity just called finish(),this activity (the parent),
* calls finish to finish the entire group.
*/
@Override
public void finishFromChild(Activity child) {
LocalActivityManager manager = getLocalActivityManager();
int index = mIdList.size()-1;
if (index < 1) {
finish();
return;
}
manager.destroyActivity(mIdList.get(index), true);
mIdList.remove(index);
index--;
String lastId = mIdList.get(index);
Intent lastIntent = manager.getActivity(lastId).getIntent();
Window newWindow = manager.startActivity(lastId, lastIntent);
setContentView(newWindow.getDecorView());
}
/**
* Starts an Activity as a child Activity to this.
* @param Id Unique identifier of the activity to be started.
* @param intent The Intent describing the activity to be started.
* @throws android.content.ActivityNotFoundException.
*/
public void startChildActivity(String Id, Intent intent) {
Window window = getLocalActivityManager().startActivity(Id,intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
if (window != null) {
mIdList.add(Id);
setContentView(window.getDecorView());
}
}
/**
* The primary purpose is to prevent systems before android.os.Build.VERSION_CODES.ECLAIR
* from calling their default KeyEvent.KEYCODE_BACK during onKeyDown.
*/
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
//preventing default implementation previous to android.os.Build.VERSION_CODES.ECLAIR
return true;
}
return super.onKeyDown(keyCode, event);
}
/**
* Overrides the default implementation for KeyEvent.KEYCODE_BACK
* so that all systems call onBackPressed().
*/
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
onBackPressed();
return true;
}
return super.onKeyUp(keyCode, event);
}
/**
* If a Child Activity handles KeyEvent.KEYCODE_BACK.
* Simply override and add this method.
*/
@Override
public void onBackPressed () {
int length = mIdList.size();
if ( length > 1) {
Activity current = getLocalActivityManager().getActivity(mIdList.get(length-1));
current.finish();
}
}
}
I got caught up trying to figure out if and how LocalActivityManager handled stepping back up the view hierarchy. After viewing the note on the LocalActivityManager.dispatchCreate(bundle state) which states "Note: This does not change the current running activity, or start whatever activity was previously running when the state was saved. That is up to the client to do, in whatever way it thinks is best."
I decided to add back tracking to the ActivityGroup. I attempted to make it behave the same as Activities outside an ActivityGroup by only needing to call finish() on an Activity and the previous Activity will be restarted or recreated.
I have also added the ability for the ActivityGroup to manage the back button being pressed. Android OS pre 2.0 handles things differently than the newer interface. A reference to this can be found here under Key Events Executed on Key-Up. I have tested the code on a device with OS 1.6 and the emulator with OS 2.1. Note: Child Activities like a ListActivity may handle the back button just override the appropriate methods for your OS version, and call onBackPressed() for the (parent) ActivityGroup. An example would look like this for OS 2.1.
@Override
public void onBackPressed() {
TabGroupActivity parentActivity = (TabGroupActivity)getParent();
parentActivity.onBackPressed();
}
The following section explains how I used this ActivityGroup. The first thing I did was subclass this ActivityGroup like this.
import android.content.Intent;
import android.os.Bundle;
public class TabGroup1Activity extends TabGroupActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
startChildActivity("OptionsActivity", new Intent(this,OptionsActivity.class));
}
}
I added this TabGroup1Activity into my TabActivity like this.
TabHost tabHost = getTabHost();
tabHost.addTab(tabHost.newTabSpec("tab1")
.setIndicator("MESSAGES")
.setContent(new Intent(this, TabGroup1Activity.class)));
Then any Activity that you want to start in the ActivityGroup can be done in a way similar to this.
Intent frequentMessages = new Intent(getParent(), FrequentMessageActivity.class);
TabGroupActivity parentActivity = (TabGroupActivity)getParent();
parentActivity.startChildActivity("FrequentMessageActivity", frequentMessages);
And that is how I added multiple Activities with a back tracking ability to a Tab of a TabActivity. Remember to always add Activities to your AndroidManifest.xml. A sample project for this article can be found here.
could you give your source code,i donot get the result
ReplyDeletewp19831014@gmail.com thank you
thank you .i have do it,but i have two question:
ReplyDelete1. how to end current activity in the ActivityGroup, if i use finish(),it end the app.
2. how to deal with back key,if i press back, it back home
i want to back activity
// Back Button Pressed Event
Delete@Override
public void onBackPressed() {
YourGroupActivity.yourGroupActivityObjectActivity.back();
}
use this method if you have minimum sdk version 2.1
@Override
Deletepublic void onBackPressed() {
YourGroup.group.back();
};
it works fine for me
Hi,
ReplyDeleteI am facing problem in launching two activities in same tab.
It will be helpful if you can pass me the source code. my email Id is dwarkesh10@gmailcom
Thank you,
Thank a lot Eric.
ReplyDeleteThis would certainly help me a lot.
-Dwarkesh
I updated the code to handle the back button. There still are several caveats including your OS version and if the child activity handles the back button. I also created a SampleTabs project that demonstrates everything in this article. Until I find a way to host it on my blogger account just leave a comment asking for it or my gmail is pretty easy to figure out eric.b.harlow. Also if this article or the sample project could help someone you know please spread the word.
ReplyDeleteCould you give source code, it's so difficult for me....
ReplyDeletemosaiclife@naver.com
Thank you
Hello,I thank you for this tutorial. A download link for source code can help reader a lot or mail me at mangal.lal@rediffmail.com
ReplyDeleteThanks,
Mangal
Hi..thanks for your great work. Could you please send the source code to my mail id gvenugopal141@gmail.com.
ReplyDeleteThanks,
Venu
Now the sample code for this article can be downloaded. See the link at the bottom of the article. Comments are still welcome.
ReplyDeleteThanks Eric..it helped me a lot.
ReplyDeleteHi Eric..i need one more suggestion from you. Could you please tell me how to give animation effect to each activity that means when we call another activity it has to come from right to left.
ReplyDeleteThanks,
venu
hi Eric,
ReplyDeleteCould you please send an exam code of this.
It will be very helpful for me
"jony.cse@gmail.com"
Your given source code not running in my pc, it shows error.
Thanks
Jony
@gvenu I don't have any experience with transition animations yet, so I may not be of much help. Look at this forum post. It may point you in the right direction.
ReplyDeleteHi Eric..again i have one problem in my application with progress dialog. The same problem will also occur in your given sample application suppose if i add progress dialog in ArrowsActivity class as ProgressDialog pd = ProgressDialog.show(getBaseContext(), "","Loading...", true);
ReplyDeletePlease give me your valuable suggestion to this problem.
Thanks,
venu
@gvenu I have not tested a dialog in the code yet, but I suspect it is the same issue as trying to have a spinner (drop down) in a child activity. They don't work yet. I do plan to look into this in the future, but I don't have time yet. I was going to start by looking into which activity child or parent is in focus and if they are both focusable. Maybe you could start there and if you discover anything I would be interested to know.
ReplyDeleteEric, your code works great, got it up and running after I read the comment from Gvenu that Dialogs were causing a problem (still haven't looked into it.) I came across another issue and was wondering if you knew about it, seems OnCreateOptionsMenu no longer works within the child activity. I suppose it may as well be a focus issue, but wanted to check with you first.
ReplyDeleteOne issue I found is a context error.
ReplyDeleteUsing getBaseContext() is an error and should be updated with getParent() in child activities and intents. All TabGroupActivity onCreate methods should update getBaseContext() to "this" -being a tabGroupActivity. Article and sample code should now reflect this change.
I have included in the sample code how to call a spinner, and a dialog as well.
Your updated code helped solve my Dialog issues, but the options Menu problem still persist. I have tried grabbing the context of getParent() in MenuInflater inflater = new MenuInflater(getParent()); but that doesn't seem to solve the issue.
ReplyDelete@Alejandro Huerta Try adding the following code to the TabGroupActivity.
ReplyDelete@Override
public boolean onCreateOptionsMenu(Menu menu) {
return getCurrentActivity().onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
return getCurrentActivity().onOptionsItemSelected(item);
}
Also implement those methods (onCreateOptionsMenu,onOptionsItemSelected) in your activity to your liking. Do not return super methods as a result, instead simply handle with appropriate true or false logic.
Thank you, Eric. That did the trick and also very useful to know that some methods get caught in the TabGroupActivity and must then be handled from there.
ReplyDeletehai Alejandro ... would you pls help me......
Deletei am still stuck on the option menu...
i am using the option menus for moving from one child activity to another..
if you already fond a solution for this problem.. can you send me the source code??
sankar.ranju@gmail.com
Hi Eric,
ReplyDeleteunfortunately, search doesn't work with this solution. Pressing the search button on the second tab throws an exception... any solution to this?
Thanks,
Stefan
@haemi I don't have a complete solution or how to, but it simply hasn't been added to the TabSample project. Android documentation does a pretty good job of explaining search. Check it out here. Two general guidelines are use TabGroupActivity as the context, and remember some methods might need to be passed through TabGroupActivity to the ChildActivity.
ReplyDelete@Eric Harlow
ReplyDeleteYou don't have to implement a search solution to reproduce the error. Even without your own search you can see that something's wrong, unfortunately. Clicking on the hardware-search-button on the first tab works, on the second the app crashes :(
I tried it out and I see what you mean. It is most unfortunate. The one interesting thing I found was it doesn't seem to be tab related. The only place it fails is in EditActivity which is a ListActivity. All of the other activities on either tab allow you to search. I have seen questions asking about ListActivity and this error. The only solution I have seen is here. I tried this with no success, so you may be out of luck.
ReplyDelete@Eric Harlow
ReplyDeleteit has to do with the listView.setAdapter()-method. If I remove that method, everything works fine. Adding just that single line => and the exception is back again. And I have NO idea why... :(
HI eric,
ReplyDeleteThanks a lot for your post.
I have few doubts, is it necessary to call onkeyup and onkeydown methods in each and every child activity and intents.
2) I have 5 tabs. from Tab 2 subactivity i have to go for tab1 subactivity, when it is tab2 subactivity i have to highlight my tab2 and when it is tab1 subactivity i have to highlight my tab1.
3) BY your code we are maintaining only one tab activities.
as android emulator your back is not functioning, any solutions to implement it.
@reddy In regards to your question about back. I am able to run the TabSample project as is without needing to have onKeyUp and onKeyDown in every child activity on a device that is OS 1.6 and emulator with OS 2.1. Are you getting an error message in TabSample when you press the back button on the emulator?
ReplyDeleteI come back here hoping you can help with this issue that I've been wrestling with. Everything in this code works fine, except that the options menu does not get recreated for each activity. If you open an options menu for a child activity, that menu will stay persistent even if you change to a different activity.
ReplyDeleteI know that the issue is onCreateOptionsMenu(Menu) is called only once per activity and TabGroupAcitivy takes up that 1 time. The previous fix to solve menu's not being created was to Override and pass along the onCreateOptionsMenu(Menu) but it will only be passed along once, thus the options menu being the same for all child activities.
I have tried using onPrepareOptionsMenu(Menu) but get a continuous loop ending in a StackOverFlow.
I'm wondering if you can provide any insight or perhaps you've already solved this issue? Thanks in advance.
hi Eric,
ReplyDeleteCould you please send an exam code of this.
It will be very helpful for me
"adahuang456@yahoo.com.tw"
@ada The link at the bottom of the article is the example code.
ReplyDeleteHi Eric,
ReplyDeleteDo you not find that your sample app leaks memory every time you attempt to destroy the activity within the activitygroup?
Would appreciate your thoughts on this...
@Atric I was unaware. Never checked for a memory leak. Could you give me more details? Also I no longer use this code myself but I would like to improve it.
ReplyDeletehi Eric,
ReplyDeleteCould you please send an exam code of this.
It will be very helpful for me
thanks....please..
"frdave@naver.com"
Hi Eric,
ReplyDeleteYour post is very useful to android beginners like me.
I have a doubt that can i use startActivityForResult() to start a child activity in our TabGroupActivity?
Thanks in advance
sathish.avunoori@gmail.com
Could you please email it to me at
ReplyDeletesheraz.jamshed@gmail.com
Hi Eric
ReplyDeletei try to use your code and it works
A question: i have 3 tab button, each one with many activity
When i start a new activity, it has the 3 tab button
how i can start a new activity without the 3 tab button? can i hide them?
Andrea
tequila77xyz@libero.it
Thanks in advance
@Andrea@Andrea
ReplyDeletei think this can just be done with startActivity() in your on click listener itself.
make sure the destination activity is not in the tab widget. By doing this it will automatically hide the tab widget.
Hi Eric,
ReplyDeleteThanks for the code :)
@Pradeep your solution work, i use this code:
ReplyDeleteIntent intent = new Intent(context, TestActivity.class);
startActivity(intent);
instead of
Intent ntent = new Intent(context, TestActivity.class);
parentActivity.startChildActivity("TestActivity", intent);
but now, when i click on the back button inside the TestActivity, to come back to the parent this code can't work:
TabGroupActivity parentActivity = (TabGroupActivity) getParent();
parentActivity.startChildActivity("TestParentActivity", new Intent(getApplicationContext(), TestParentActivity.class));
how i can set the parent inside TestActivity ? i look for this method
intent.putExtra
but this method accept only String so i can't write this code
intent.putExtra("TestParentActivity", TestParentActivity);
@Andrea I have not tested this to be sure but something that may help your back button issue would be to override onBackPressed() in your TestActivity if your are running OS 2.0 or greater. If you are running on an OS version before that (1.5,1.6) override onKeyDown and onKeyUp. The key thing to guarantee in either of these methods is to call finish() on TestActivity. If that doesn't work there is the method intent.putExtras(Intent) that may help the other thing you are trying.
ReplyDeletehi,could you give your source code?
ReplyDeleteMy email is zhang096608@gmail.com
i created an activity group which activity 1 extend to it and set the onBackPress() in activity 2. but when i click on the back key while i am in activity 2, nothing happen. . .
ReplyDeletecan someone tell me how to solve this?
awesome ... thanks
ReplyDeleteThanks.. It is very usefull..
ReplyDeleteKeep doing good work. Best luck..
Thanks,
PiysuhNP
Hi, Eric Harlow. Thanks for this tutorial. But i have a problem, there is a EditText in one root tab activity, when the softkeyboard is open, then press back button, the acitivity will be exited instead of hiding the softkeyboard. Please giving me some clues, thanks again.
ReplyDeleteI'm curious if you handle orientation change any special way, or are you locked into either portrait/landscape. I'm trying to find a solution for transitioning the current state of the app across orientation changes.
ReplyDeleteThanks.
there are known issues while implementing startActivityForResult inside the tabs.Can anyone help override the setResult of startActivityForResult??
ReplyDeleteEven i am facing problems with startActivityForResult() and memory leaks while using this code. Please share if you find a solution.
ReplyDeletehi friend.. can i get this source code
ReplyDeletejeeva_engg@rediffmail.com
Hi, I tried to use this example with an activity and a Listview populated by a simplecursoradapter. Then thing is that when It keeps crashing because ANR.Do you know how to implement this with a simplecursoradapter?
ReplyDeleteI´m also curious how to handle orientation changes.
ReplyDelete//Henrik
@Danny Palmer
ReplyDeleteDid you manage to solve the orientation issue?
Hi thanks for this it help me lot.But me having some question in it like
ReplyDeletei am having three tabs tab1,tab2,tab3.View's to each tab is set.After click on tab1 it should go to the activity from where it is launch.
e.g activity1 =>activity2(tab1 is set for this activity) .Now i want after click on tab it should go to activity1 from activity2
Hi
ReplyDeleteI wounder if you could help me with some coding issue I have?
Im trying to get a IMG button to open a new view from a tab. but when I implemet the onclick listener ( or any other way) the app crashes.
Got any Idea?
markman.electronics att gmail.com
@gvenu did you manage implementing the transition animation taking Eric multiple activities within tab approach? because I've been trying to implement this without any success
ReplyDeleteThanks for the code. Works great for me.
ReplyDeleteI was wondering, is it possible to open a tab from another tab?
thanks 4 the code.....hi..u put the progressbar here.............but i want a Alertdailog with listview inside the tab.....can u please suggest???????????????????
ReplyDeleteHi,
ReplyDeleteIt gives above error because of the Context I was providing to alertdialog builder.
Give:
new AlertDialog.Builder(getParent()).setMessage(“Hello world”).show();
We just need to call getParent() instead of AlertDialog.Builder(this)
Thank you very much for this. It worked like a charm.
ReplyDelete@Blessan I am glad it is helpful. Hopefully I will be able to release a newer version this month with some updates and improvements.
ReplyDeleteHi Eric....
ReplyDeleteAwesome man.....its working great...
Thanks for the code..
Hi, your tutorial is wonderful, and is there anyway I can have an animation for transition from one activity to another, both within the activity group?
ReplyDeleteThanks!
Hello Eric;
ReplyDeleteI am working on same application. But i can't use back button. I have 3 tabs. Every tab has 4 activity(such as; A , B , C, D). i wanna go to A->B->C->D activity. And i wanna go back to D->C->B->A with back button. But back button can do this.
I haven't solved this problem for 3 weeks.
Please help me!
I'm sorry my english is not good...
Hi Eric...thanks for this awesome tut..
ReplyDeletebut I am facing "Force Close" error, when click on spinner which is in a child activity of parent activity in a tab. I am using this code ----
COUNTRYLIST = getIntent().getStringArrayExtra("COUNTRYLIST");
COUNTRYID = getIntent().getStringArrayExtra("COUNTRYID");
countries = (Spinner) findViewById(R.id.leadCountry);
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, COUNTRYLIST);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
countries.setAdapter(adapter);
countries.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView parent, View v, int position, long id){
lcountry = parent.getItemAtPosition(position).toString();
lcountryid = COUNTRYID[position];
}
public void onNothingSelected(AdapterView parent){
}
});
hi its great article. I am having a little confusion. If u send the source code then it will helpful for me. My email id :harish.grandhi@gmail.com
ReplyDeletecould you sedn me the source code please. jkerns119@gmail.com
ReplyDeleteThanks
PEFECT, i did add a finish() command from the overridden method onBackPressed since you don't let the user go back to the Android home screen, when they pressed the back key, anyway everything is working perfect...
ReplyDeleteOrientation resets everything though, thinking to do on OnResume...
ReplyDelete@Michael Angelo
ReplyDeleteCan you send the modified code?
In my application, the back button leads to the android's home screen.
When pressing back from child activity the application closed and the home screen displayed.
ReplyDeleteHow it can be fixed?
A simple way to fix that is to override finishFromChild(Activity child) and call popTabContent() in it. Be careful with that as I ran into inconsistency issues and ended up overriding finishFromChild() and onBackPressed(). I didn't implement anything in either of this methods and instead call my popTabContent() in onKeyUp(int keyCode, KeyEvent event) like this
ReplyDelete/**
* Overrides the default implementation for KeyEvent.KEYCODE_BACK
* so that all systems provide a more consistent behavior.
*/
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
popTabContent();
return true;
}
return super.onKeyUp(keyCode, event);
}
Can you send the popTabContent() code?
ReplyDeleteHi,
ReplyDeleteusing this tutorial I got the following error:
"Unable to start activity component info".
Anyone know how to solve?
Thank you so much Eric your tutorial is very useful for my college work. God bless you man.
ReplyDeleteExcellent post dude, really helped us out!
ReplyDeleteExcellent tutorial but i have one problem:
ReplyDeletethe TabActivity have a ActivityGroup that starts a new activity and this activity begins another activity.
My problem is when the activity is paused automatically closes the application.
ERROR/AndroidRuntime(7378): FATAL EXCEPTION: main
ERROR/AndroidRuntime(7378): java.lang.NullPointerException
ERROR/AndroidRuntime(7378): at android.widget.TabHost.dispatchWindowFocusChanged(TabHost.java:317)
ERROR/AndroidRuntime(7378): at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:662)
ERROR/AndroidRuntime(7378): at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:662)
ERROR/AndroidRuntime(7378): at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:662)
ERROR/AndroidRuntime(7378): at android.view.ViewRoot.handleMessage(ViewRoot.java:1923)
ERROR/AndroidRuntime(7378): at android.os.Handler.dispatchMessage(Handler.java:99)
ERROR/AndroidRuntime(7378): at android.os.Looper.loop(Looper.java:123)
ERROR/AndroidRuntime(7378): at android.app.ActivityThread.main(ActivityThread.java:4633)
ERROR/AndroidRuntime(7378): at java.lang.reflect.Method.invokeNative(Native Method)
ERROR/AndroidRuntime(7378): at java.lang.reflect.Method.invoke(Method.java:521)
ERROR/AndroidRuntime(7378): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
ERROR/AndroidRuntime(7378): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
ERROR/AndroidRuntime(7378): at dalvik.system.NativeStart.main(Native Method)
Great tutorial! I have it working for basic functionality, but I'm having trouble with Animations. I have some animations that I want to run until the end, then once they're done, stay in their finished state. When another activity is started, I want them to remain in their finished state when the back button is hit, and returned to the Activity hosting the animations.
ReplyDeleteThe problem is that since the activities are started all over again, the animations run from the beginning each time the Activity is brought to the front again.
Can you think of any way to mimic the normal behavior of activities, where the activities are "paused" and "resumed" when they lose and gain focus on the activity stack?
Update: Actually, all I had to do was configure the activity with the animations to use android:launchMode="singleTop", so when the LocalGroupManager fired off the intent, the same instance of the activity was used instead of creating a new one.
ReplyDeleteHow do you assign a unique id to an activity?
ReplyDeleteThanks for this solution, but I have one problem, same at mnemy in fact... but me, I can't set my activity to launchMode="singleTop" to resolve the problem, because I need multiple instance of the activity. Have you a solution for that ?
ReplyDeletei am using ure source code for my application.
ReplyDeleteI am not being able to create alert dialog.
AlertDialog.Builder saveDialog = new AlertDialog.Builder(Setting.this);
saveDialog.setTitle("Save Settings");
saveDialog.setMessage("Are your sure?");
saveDialog.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
saveSettings();
Toast.makeText(getApplicationContext(), "Your settings has been saved.", Toast.LENGTH_LONG).show();
}
});
saveDialog.setNegativeButton("No",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
clearFields();
}
});
saveDialog.show();
my error : badtokenexception
Thanks for the solution. Everything is working perfectly except the menu option. As mentioned by MetaStable, the option menu will be the same for all child activities once it enters the child's activities, it will not change back to the parent menu options even if the user change to other activities.
ReplyDeleteIs anyone found any solution on this problem? Thanks.
@Robbins
ReplyDeleteAlertDialog.Builder saveDialog = new AlertDialog.Builder(getParent());
Thx. that is very good example ^^
ReplyDeleteThere is a pretty big bug - if you start from activity A an activity B and then finish() in activity A, the code above will actually finish activity B.
ReplyDelete@Artem Russakovskii If you look closely at the method finishFromChild(Activity child) you can see that I designed it as a FILO stack. So when a finish() is called this method is current ending the activity on the top of the stack. If you want the behavior you mentioned I would suggest changing the implementation of finishFromChild.
ReplyDeleteHi Eric,can you please tell me how can I use startActivityForResult() from my child activity?
ReplyDeleteHi all, can someone please tell me how we can use startActivityForResult() from one of loaded activities? When I use that method, my onActivityResult() is not getting called.
ReplyDeleteHi Eric, great job. I used ur code to start several activities in one tab. But i have a question. If the user press the backbutton u recreate the last Activity with:
ReplyDeleteString lastId = mIdList.get(index);
Intent lastIntent = manager.getActivity(lastId).getIntent();
Window newWindow = manager.startActivity(lastId, lastIntent);
setContentView(newWindow.getDecorView());
Why do you so? In my case every time i press the back button my latest list will be recreated. So the activity call the server, get the response and show the list. Isn't this unnecessary?
You could instead get the contentview from the last activity like this
setContentView(manager.getActivity(lastId).getWindow().getDecorView());
In one sentence: I does not understand why the activity has to be recreated instead of reused? It would be nice if u could explain that. ;)
Greets from Germany
This comment has been removed by the author.
ReplyDeleteOk, the problem above is because of the Flag "FLAG_ACTIVITY_CLEAR_TOP". If i remove this previous activities are resumed instead of created. I thinks this is more comfortable. Do u agree?
ReplyDeleteHey Benjamin,
DeleteAm facing the same prob of the activity being recreated on pressing back....I want the activity to get resumed.
Can u please point me in the right direction?
I have fixed some issues,you can refer to my code:
Deletehttps://github.com/pjq/ActivityGroupDemo
Hay Benjamin you got the point man..Thanks alot
Deletelet say if i remove FLAG_ACTIVITY_CLEAR_TOP then i can come back but if i again open the same/recent activity(activity from where i just came back) would not be open.
Deleteany solution regarding this?
This worked perfectly! You're the man!
ReplyDeleteHellO! I am trying to use your code to call a camera intent but I am unsure how to use your code. Any ideas?
ReplyDeleteYou Sir, are my hero. I've been looking for days for something that solves this particular problem. I've posed my questions multiple times on IRC without any reply.
ReplyDeleteFor the people above who can't get it to work: work this example out in a sample project and move on from there. This is as clear as water and as clear as he can make it.
Thanks again for this life-saving post!
I'm having some trouble using the presented approach. I have an EditText on activity A, and when I start a child activity B (inside the same tab) and later return to activity A, the soft keyboard won't show up anymore.
ReplyDeleteWhen switching to another tab and returning to the original one, the keyboard starts working again.
Any ideas? Thank you!
Hi Gustavo, did you find the answer for this problem?
DeleteSadly no! I ended up modifying my app's screen flow and now I plan to never use a tab bar again. :)
DeletePlease, send me sources on slavadev@ukr.net
ReplyDeleteThanks in advance)
Thanks for sharing, it works great!
ReplyDeleteCould you send me the sources, please? This is my email address, thank you for your contribution:
ReplyDeleteluk2.86tk@gmail.com
regards.
I have a question, I have two Tabs A y B, inside Tab A i have activity 1, activity 1 launches activity 2, then i select Tab B with activity 3 then return to Tab A, but Tab A display activity 1 when the last activity was activity 2, why is this happenning?, how can i mantain the activity tab state ?.
ReplyDeleteBest Regtards.
Regarding the issue with onActivityResult
ReplyDeleteThe solution is override onActivityResult at TabGroupActivity and then route the received values to the current activity
Activity current = getLocalActivityManager().getCurrentActivity();
Hi, thanks for confirming where to capture the intent result. However: exactly how are you supposed to trigger the onActivityResult on the current (real, not TabGroup) activity?
DeleteIf you in the @Override TabGroupActivity.onActivityResult try the following
Activity current = getLocalActivityManager().getCurrentActivity();
current.onActivityResult(reqCode, resultCode, data);
you notice that onActivytResult is NOT visible. It's protected and thus the TabActivity does not have access to it. I do not want to write another metamethod and ugly casting (to your specific Activity) which would add additional overhead to the scenario.
This whole TabGroup & Activity intercommunicatin issue is starting to cost alot more than it tastes. Has any1 tried simply having one Activity per tab and handling all subviews as simple views which you switch out? After all the issues I'm most def. leaning towards such a solution.
Hi i use your class TabGroupActivity class your but when i click the back button of the child activity it reload the activity i don not want to reload that i only want to display its previous state.same as normal android application can do.
ReplyDeleteplease help me as early as possible.
@Hiram Fernandez
ReplyDeletehi Hiram.
Can you explain abit about onActivityResult problem. Like how to call startActivityForResult, how to make onActivityResult fire =.=
Thanks you.
Thank You .... :)
ReplyDeleteNo words I searched Everywhere.
Mahe
Hi Eric,
ReplyDeleteI am using the Activity group for designing Tab bar.I am facing one small issue that is "I have list view in my first tab.when I click on the list item I am going to another activity(act2). when I press back button from 'act2' act2 is closing and at a time First Tab activity also closing.
Note:It is happening 2 times out of 12 times.
Could you please give solution for my problem?
Hi I have a question. If i want the user touch the tab every time start first activity not the child what leave before then i where recreate the mIdList?
ReplyDeleteMy idea if leave the tab to other tab then i clear the mIdList and if the user back the tab then show the first activity :) Just where clear?
thx
Thank you very much for this article...... from 3 days i tried and searched for concept..... here i got..... Thanks again
ReplyDeleteHello!
ReplyDeleteI have small problem with sample! If i click to "Next" button on option page and rotate the screen to "Landcape mode", app go automatically back to first activity in group. Whay this?
Hi,
ReplyDeleteThe tutorial is of great help.
But I am facing an issue with saving the state of an activity. Say I am in Activity A with some content in it which is fetched from web service. Now, on a button press from Activity A the app goes to Activity B. Coming back to Activity A should have the same content that was there but it do not happens so. Can you please suggest a way of saving the state in activity group.
Thanks
Sunil
Hi sunnil.softweb! I think we got the same problem... if you find the solution, please leave comment here!!! Thanx!!!
ReplyDeleteHi Nevro,
ReplyDeleteI am looking into it and it seems the startChildActivity method in TabGroupActivity sets the clear top flag and may be that's the reason it behaves in such a manner. I tried by removing that flag but then the list item onItemClick is called only once which is really strange.
I will surely post here if I find anything.
Thanks
Sunil
Hi i have been using your code as the base of my application
ReplyDeleteand I have structure like this
MainActivity -> TabGroupActivity1 -> Activity1 -> Activity2.
now I want to redirect on activity2 only if the flag is true
i am using something like this
if(flag){
Intent previewMessage = new Intent(getParent(), ArrowsActivity.class);
TabGroupActivity parentActivity = (TabGroupActivity)getParent();
parentActivity.startChildActivity("ArrowsActivity", previewMessage);
}else{
setContentView(R.layout.row);
next = (Button)findViewById(R.id.button1);
Button back = (Button)findViewById(R.id.button1);
}
but it is not working If i redirect on some click events it work but if i do somthing like this it is not working
Please help me with this
Thank you
Hey,
ReplyDeleteThanx for the great tutorial.
Although everythings working jus fine but in one of the child activities i am using spinner(for drop down) and a timePickerDialog.
On clicking the spinner and the timepickerdialog i am getting this error
----------------------------------------------------------
02-04 17:40:01.993: ERROR/AndroidRuntime(354): android.view.WindowManager$BadTokenException: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord@44f03200 is not valid; is your activity running?
----------------------------------------------------------
Could anyone please help me out.
Thanks & Regards.
Hi Mohamad,
DeleteI'm also facing the same problem.
Can you update me, if you find any solution for this?
Regards,
Gopalan
If you are creating your spinner/timePickerDialog using "this", change "this" to "getParent()"
DeleteWorked for me...
Hey there,
ReplyDeleteThis is really a great tutorial. I just wanted to say thanks.
Keep up the good job.
Found solution for doing animations when switching Activity in ActivityGroup. Just add below methods in your custom ActivityGroup class
ReplyDeletepublic Animation inFromRightAnimation() {
Animation inFromRight = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, +1.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f);
inFromRight.setDuration(500);
inFromRight.setInterpolator(new DecelerateInterpolator());
return inFromRight;
}
public Animation outFromLeftAnimation() {
Animation outFromleft = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, -1.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f);
outFromleft.setDuration(500);
outFromleft.setInterpolator(new AccelerateInterpolator());
return outFromleft;
}
and put
window.getDecorView().setAnimation(inFromRightAnimation());
before setContentView(window.getDecorView());
in both startChildActivity and finishfromChild methods
Here is a ActivityGroup Bug
ReplyDeleteSee the details:http://code.google.com/p/android/issues/detail?id=10083
The following methord is wrong in LocationActivityManager:
public Window destroyActivity(String id, boolean finish) {
LocalActivityRecord r = mActivities.get(id);
Window win = null;
if (r != null) {
win = performDestroy(r, finish);
if (finish) {
mActivities.remove(r);
}
}
return win;
}
https://github.com/pjq/ActivityGroupDemo
ReplyDeleteHas anyone fixed the issue with the search dialog not displaying when you click on the search hardware button?
ReplyDeletehi is this method deprecated? , is it better to go with fragments for this kind of task? thanks ;)
ReplyDeleteThank you very much, very helpful..
ReplyDeleteHi...
ReplyDeleteI hav a pblm on my code,pls give me solution...
I created two tabs on main activity, on clicking the first tab it shows the listview1 and cliking the second tab it shows the other listview2... i dnt knw how to do... pls give me the code for it..
ActivityGroup is depricated do you have the same example using Fragment and FragmentManager
ReplyDeleteSoooooooooooooo nice and good. it helps me a lot.
ReplyDeleteand better to go the following links there you can some code
https://github.com/pjq/ActivityGroupDemo
http://stackoverflow.com/questions/1306689/launching-activities-within-a-tab-in-android
i'm very thankful to you as i was stuck with the same problem.good work.
ReplyDeleteHello Eric,
ReplyDeleteI am facing problem in launching two activities in same tab.
It will be helpful if you can send me the source code. my email Id is satishkolawale2@gmail.com
Thanks
On pressing Back key no events occurs.I want to get back to menu page.
ReplyDeletethanx a useful tutorial...
ReplyDeletebut i have a doubt . iam implementing a option menu.. on one of the child activity...
on the item click i start another child activity... but its not woking...
how the option menu is implemented on the activity group.
can u mail me source code of this program..
ReplyDeletem not able to run it..
my mail id is nik.shah41@gmail.com
Thank you so much for your tutorial,
ReplyDeleteI have bugs with this code. Whenever I click back button my app goes to force close state. I'm tried 3 days to clear this bug but not able clear it. Please help me and I was test this with Android OS 4.0.4. Thanks in advance.
Intent lastIntent = manager.getActivity(lastId).getIntent();
ReplyDeleteThis line gives NullPointerException while I'm tried to go back by pressing back button.
Since you are using Android 4.x ActivityGroup is deprecated. I would suggest you use the ActionBar, FragmentManager, and Fragments instead.
ReplyDeleteHi i am tab activity and i am facing some prob.. please help me.
ReplyDeleteI have two tabs Tab1,Tab2.
Tab1 contains Activity1 which has a listview ,so when i click on the listview items, it should open another activity which contains some data .i want that data to send back to Activity1 .
Now i am able to send the data back to Activity1 but both tabs are missing. I want both tabs Tab1,Tab2 should be there. I have already used ActivityGroup class along with replacecontentview(),but that did not helped.
Thank u some much for this awesome tutorial..It helped me a lot...
ReplyDeleteHi Eric,
ReplyDeleteIt's really a great tutorial to enjoy. I used your tutorial to launch my activities in TabGroupActivity.
Please help me for the below situation. I am stuck here :(
I launched my child activity as given below from the first activity:
try {
TabGroupActivity parentActivity = (TabGroupActivity)this.getParent();
Intent intent = new Intent(InstagramMainActivity.this,InstagramPhotoDetailActivity.class);
intent.putExtra("Index", (int)adapter.getItemId(position));
parentActivity.startChildActivity("InstagramPhotoDetailActivity",intent);
}
catch (Exception e) {
Intent intent = new Intent(_myActivity, InstagramPhotoDetailActivity.class);
intent.putExtra("Index", (int)adapter.getItemId(position));
startActivityForResult(intent, 1);
}
And then in second activity again I used the same above code, but with different activity name, and for the third one. So now my mIdList contains 3 activities. But as soon as I launched third activity from the previous one, its not getting started. I tried to debug and come to know that my third activity's onCreate method is being called three times, i.e. number of elements in the mIdList and my app crashes.
Plese suggest me the ideal way of keep launching the child activities from the parent and so on.
Also if I back pressed the button, instead of going back to previous parent activity, my activity comes back to home screen, i.e. first activity of the TabGroupActivity
Waiting for your kind reply..
Thanks
Hi Eric,
ReplyDeleteI have a case where i need to open a mapv2 fragement activity from a listview in a tab,wen i cick the tab,listview comes first and after clicking the listview,the map shows all wraps inside one tab is that possible to do that in tabgroup activity,cause my map shows a white screen,but it opens wen called without using tabgroup,please help me i am stuck here
could you please send me your source code, i cant get hold of it.
ReplyDeletesfaisalawan84@gmail.com
Hi Eric, I followed your blog here and tab contents are working fine. But issue with Async task not populating a ListView as it cannot find token window or related to Context not right? What do you think?
ReplyDeletehttp://stackoverflow.com/questions/16758401/android-how-to-add-sherlock-actionbar-to-slidingactivity-and-progressdialog-sh
I am at sebastian_cheung@yahoo.com if you could reply directly it would be highly appreciated.
DeleteHello,
ReplyDeleteI am using TabGroup in my application. There are four tabs, Tab1, Tab2, Tab3, Tab4. Default tab is Tab1.The normal flow in Tab3 is Page1 -> Page2 -> Page3 -> Page4 where Page1 extends TabGroupActivity. My application can also receive push notifications. So when a notification is received I want to open page4 in Tab3. And on the back button of page4 the flow should be Page4 -> Page3 -> Page2 -> Page1. Any idea, how to implement this?
This comment has been removed by the author.
ReplyDeleteThanks eric harlow..you made my day man..Thanks alot
ReplyDeleteI got total 4 tabs. My 2nd and 3rd tab cannot finish(). Reason?
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteHi can you mail me the source code .... please
ReplyDeletesmriti005005@gmail.com
Regards::
smriti
Thanks a for sharing knowledge really appreciating !!!
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteHi..
Deletewhen onBackPressed() call at that time previous activity is destroy and call it's all life cycle..
onCreate()
onStart()
onResume()
onStope()
onDestroy()
but i want, When user onBackPressed()
at that time user not destroy previous activity but Resume previous acivity..
please help me.
Thank You,
Bhavesh Vadalia
Hi..thanks for your great work. Could you please send the source code to my mail id ahirvishnu@gmail.com
ReplyDeleteHi all
ReplyDeleteIf I want intent from Activity to ActivityGrop how i do ?
Very well buddy. Very Usefull & truthfull demo till this date.
ReplyDeleteKeep it up.
GREAT !!!
ReplyDeleteThanks Friend .... This Working like Charm....
ReplyDeleteGood work dude..
ReplyDeleteGood work dude..
ReplyDeleteHi Can you please send me the source code.It will be gr8 help for me
ReplyDeleteid : anujacomp25@gmail.com
Hi can you please send mt the source code,
ReplyDeleteactually i have 3 tabs , on 1st tab i want to press a button and want to see the vedio file on vedioview?
Please send it to niteshkumar80@gmail.com , thanks for your help.
Hi Can you please send me the source code,
ReplyDeleteid:agarwal.mayank32@gmail.com
Thanks for your help.
This comment has been removed by the author.
ReplyDeleteSuperb. I really enjoyed very much with this article here. Really it is an amazing article I had ever read. I hope it will help a lot for all. Thank you so much for this amazing posts and please keep update like this excellent article.thank you for sharing such a great blog with us. expecting for your.
ReplyDeleteAWS Certification Training in Chennai
AWS Training in Bangalore
AWS Training in Thirumangalam
AWS Training in Vadapalani
I was recommended this web site by means of my cousin. I am now not certain whether this post is written through him as nobody else recognise such precise about my difficulty. You're amazing! Thank you!
ReplyDeletepython training in velachery | python training institute in chennai
It has been simply incredibly generous with you to provide openly what exactly many individuals would’ve marketed for an eBook to end up making some cash for their end, primarily given that you could have tried it in the event you wanted.safety course in chennai
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDeleteNice blog..! I really loved reading through this article. Thanks for sharing such a amazing post with us and keep blogging...Well written article on android activities Thank You Sharing with Us.android development for beginners | future of android developer in india |
ReplyDeleteandroid device manager location history
This comment has been removed by a blog administrator.
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDeleteReally you have given a good information.. Good points you have given about android activities. I hope you keep posting like this with good information.
ReplyDeleteThanks
Katherine
(TekSlate)
This comment has been removed by a blog administrator.
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDeletenice post...
ReplyDeleteit course in chennai
it training course in chennai
c c++ training in chennai
best c c++ training institute in chennai
best .net training institute in chennai
.net training
dot net training institute
advanced .net training in chennai
advanced dot net training in chennai
It was Informative Post,and Knowledgable also.Good Ones
ReplyDeleteplanet-php
Technology
Magnificent blog!!! Thanks for your sharing… waiting for your new updates.
ReplyDeletePHP Training in Chennai
PHP Course in Chennai
PHP Training in Coimbatore
PHP Course in Coimbatore
PHP Training in Bangalore
Your information's are very much helpful for me to clarify my doubts.
ReplyDeletekeep update more information's in future.
AWS Training in Thirumangalam
AWS Training in anna nagar
AWS Training in Vadapalani
AWS Training in Nungambakkam
My spouse and I love your blog and find almost all of your post’s to be just what I’m looking for.
ReplyDeleteiosh safety course in chennai
Nice post. By reading your blog, i get inspired and this provides some useful information. Thank you for posting this exclusive post for our vision....
ReplyDeletedata science online training
sas online training
linux online training
aws online training
testing tools online training
devops online training
salesforce online training
nice post..
ReplyDeleteseo training in chennai
seo training institute in chennai
erp training institute in chennai
erp training in chennai
tally erp 9 training in chennai
tally erp 9 training institutes
android training in chennai
android training institutes in chennai
mobile application testing training in chennai
And indeed, I’m just always astounded concerning the remarkable things served by you. Some four facts on this page are undeniably the most effective I’ve had.
ReplyDeleteiosh safety course in chennai