I am making an application with tabs. In that app same TabView is to be shown in multiple activities in hierarchy. For that i used ActivityGroup.
In my application i can navigate from first activity containing tab to its child activity and can come back to previous activity by pressing a button in child activity. While navigating between these two activities, i get StackOverflowError after few navigations.
I tried flag
Intent.FLAG_ACTIVITY_CLEAR_TOP
but it doesn't help.
I also tried
finish()
but it finishes whole ActivityGroup.
Then i tried method
finishActivityFromChild()
but still getting same error.
This is my code for moving from first activity containing tabs to its child-
intent = new Intent(context, ChildActivity.class);
View view = getLocalActivityManager().startActivity("activity2", intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)) .getDecorView();
setContentView(view);
finishActivityFromChild(getCurrentActivity(), 0);
And the same code i am using for coming back to parent activity on click of a button-
public void onClick(View arg0) {
intent = new Intent(context, ParentActivity.class);
View view = getLocalActivityManager().startActivity("activity1", intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)) .getDecorView();
setContentView(view);
finishActivityFromChild(getCurrentActivity(), 0);
}
Now i have no idea what to do for this problem. Any help is appreciated.
Thanks in advance.
I think some code would help here. If I were you, I would try to plent printouts of functions called, to see what function is being called recuresively that might cause the stack overflow. (Some breakpoints might also do the trick)
Related
I've been working on this problem now for several hours but still couldnt figure it out.
I have a MainActivity with several Tabs, which are all linked to their own fragment. The Tabs and their fragments are all working perfectly fine.
Main Activity with different Tabs(Fragments):
When I switch to the second Tab you can see a button.
MainActivity and Tab2(Fragment1):
Through the button "Theorie" you are navigated to the SecondActivity.
SecondActivity:
Now there is the following problem. I would like to return to Tab2 in the MainActivity. But because this is the second fragment of the MainActivity, I always get transfered only to the first Tab "Home" in the MainActivity with the following code.
`
ImageView homebtn = (ImageView) findViewById(R.id.homebtn);
homebtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(),MainActivity.class);
startActivity(intent);
}
});
`
I really don't know, how I can get through the homebtn to the second Fragment "Tab2" instead of the first Fragment "Home" in the MainActivity. When I conducted a research about this problem, I only found solutions how to transfer data from on fragment to another or how to get from the MainActivity to its fragments. I couldn't find any solutions for my specific problem.
If you have any advice for me how to solve this problem, I would be very thankful :)
Greetings
Chris
When you are going to the Second activity, don't finish the first activity. And on home button just use this :-
finish();
Instead of:-
Intent intent = new Intent(getApplicationContext(),MainActivity.class);
startActivity(intent);
You are finishing the first activity in the process and starting a new activity altogether which resets everything.
Hope this helps.
I have implemented a TabHost. In one tab I have Activity1, which calls Activity2 after a button click, which calls Activity3 after a button click, which calls Activity1 after a button click, etc.. No backstack functionality is required, just 1 --> 2 --> 3 --> 1, etc. All three activities have a separate layout file.
Everything works fine, except that after the first transition from 1 --> 2 the activities grab the entire screen and the tabs are invisble forever.
Question: how can I keep these three activities within the confinement of de tab area and the tabs visible? The problem has been recognized here many times before; the solution used to be ActivityGroups, but these are deprecated and Fragments are advised instead. I have seen many examples here, but nothing that could help me.
Can I keep my three activites (Activity1 extends Activity, etc)?
Should I add fragment tags to the layout files?
Do I need to work with transactions?
Should I work with one fragment class or three?
Can you please give me a few hints how I should go about? I woud already be helped if you tell which classes I need to use and of what type they are.
Thanks in advance.
It took me more than half a day, but finally found a solution that works. Unfortunately I am still stuck with deprecated issues (Activity Group and getLocalActivityManager().startActivity(..)).
Again I have a single tab under a TabHost and several activities, all operating within that tab. Navigation from one activity to the next occurs with a buttonclick. Solution:
all Activities operating within the tab need to extend ActivityGroup
All Activity classes need to have a button handler that links to the next activity like this:
public void onBtnClicked(View view) {
Intent intent = new Intent(view.getContext(), NextActivity.class);
replaceContentView("NextActivity", intent);
}
public void replaceContentView(String id, Intent newIntent) {
View view = getLocalActivityManager().startActivity(id, newIntent.
addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView();
this.setContentView(view);
}
By this the tabs remain visible all the time, as desired.
Hope this helps someone.
i have multiple activities in my activityGroup under tabactvity.
I want to start the child actvity for result.
Intent i = new Intent(this, addstocks.class);
View view = stocksgroup.group.getLocalActivityManager()
.startActivity("show_city", i
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
.getDecorView();
// Again, replace the view
stocksgroup.group.replaceView(view);
this code basically taking me to the next activity. here i want to implement the actvity for result. hw can i do that?
It ts very late but just for reference a demo can be seen here: onActivityResult is not been invoked after the child activity calls finish()
I have created two tabs, say TAB1 and TAB2. For TAB1, i have loaded one Activity, say ActivityOne, into TAB1 as
Intent intent = new Intent(this,ActivityOne.class);
TabHost.TabSpec spec = getTabHost().newTabSpec("ActivityOne")
.setIndicator("Activity One",getResources().getDrawable(R.drawable.artists)).setContent(intent);
getTabHost().addTab(spec);
This ActivityOne has extended the ActivityGroup and i added one button in this activity. By clicking on this button it will call another activity, say ActivityOne_One, as
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(ActivityOne.this,ActivityOne_One.class);
replaceContentView("ActivityOne_One",intent);
}
public void replaceContentView(String id, Intent intent){
View view = this.getLocalActivityManager().startActivity(id, intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView();
this.setContentView(view);
}
When we click on that button ActivityOne_One will be launched under same TAB1. In this application i have two problems:
1) If i want to go back to ActivityOne under same TAB1 by using traditional BACK button on emulator it is not working..
2)ActivityOne_One is launching with no animation(like sliding from right to left) effect.
If anyone know about any one of these, give your advice..
Thanks,
venu
I have found the solution for my question. The following blog gave me a route for this
http://ericharlow.blogspot.com/2010/09/experience-multiple-android-activities.html
see this link which may help you out with this. http://blog.henriklarsentoft.com/2010/07/android-tabactivity-nested-activities/
i have implemented what is advised in the link i provided. it works OK, but it is quirky and does not always redirect you BACK like you would expect. i have found that you must implement your own custom tabs to really get this desired effect.
There are 4 Tabs in a TabHost, let them be A, B, C, and D. Now each one is just an index page and clicking on any of them shows a different activity.
The problem is that I need to start another activity when the user selects something from the content displayed in the tab. The other activity should also be displayed in the parent tab itself. Is it possible? Or will I have to try something else?
Try this, found this solution in android cookbook,
http://androidcookbook.com/Recipe.seam;jsessionid=5424397F3130CE7769FF47DD67742911?recipeId=1693&recipeFrom=ViewTOC
Can't you change the contentView of your tab instead of starting a new Activity ?
Maybe I'm wrong but I think also that starting an activity in a tab isn't possible because the TabView is hosted in a activity and not the opposite (Tabview don't host an activity per Tab).
I think the common consensus is that it is best not to use individual Activities as tab content due to these limitations. See these questions and answers for pointers to alternatives:
Android: Why shouldn't I use activities inside tabs?
Android - Tabs, MapView, activities within tabs
To summarize the link that Rukmal Dias provided. Here's what you do:
Change your current Activity (that's in a tab) to derive from ActivityGroup
Create a new intent for the Activity you want to switch to
Copy/Paste and call this function in your current activity where "id" is the "android:id" for the layout of the new activity you want to switch to
public void replaceContentView(String id, Intent newIntent){
View view = getLocalActivityManager().startActivity(id,newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)) .getDecorView();
this.setContentView(view);}
Here's an example of how I make the call to switch views from my current Tabbed Activity:
public void switchToNextActivity(View view)
{
Intent myIntent = new Intent(getApplicationContext(), MyNextActivity.class);
replaceContentView("next_activity", myIntent);
}
It looses the view hierarchy. When you press the back button, in my case, the app closes.