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()
Related
What I am trying to do is kept all the data in a child activity and go back to a parent activity .
In other words, as far as stack structure, the child activity, which is located at the top of stack structure, should be kept and go back to parent activity.
Thank you for your help in advance
One method you could use is starting the child activity using startActivityForResult(). Then once the child activity is closed, data is passed back to the parent activity.
If you then want to start the child activity again with the same data as before, you can check to see if the parent activity has any data from a previous result and parse it to the new child activity in the intent.
ParentActivity
String mData = null; //this goes above the parent's onCreate method, using a string as an example
//this is where you start the child activity
Intent intent = new Intent(context, ChildActivity.class);
if(mData != null) {
intent.putExtra("MyData", mData);
}
startActivityForResult(intent, mRequestCode);
ChildActivity
onCreate...
if(getIntent().getStringExtra("MyData") != null) {
//handle restoring the child activity to its previous state
}
In you parent activity you could even get rid of the if statement as the child activity checks if the data sent is null.
There can be only one active activity at one moment of time. If you need to save state, serialize it to SharedPreferences or to your Database
I suggest to use Fragment instead of activity. It is a better way I think than using activity.
I am using ActivityGroup. I use the following code from ActivityGroup in order to replace view and launch a new activity.
Intent i = new Intent(SummaryCostScreen.this,PermissionsScreen.class);
replaceContentView("activity1",i);
public void replaceContentView(String id, Intent newIntent) {
View view = getLocalActivityManager().startActivity(id,
newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView();
}
The problem with the above code is that, I need to have startActivityForResult in place of startActivity, since I need to update the UI of launcher activity when coming back from the launched activity.
getLocalActivityManager() does not have startActivityForResult. How should I address this situation, such that, I am able to update UI from onActivityResult?
Any help is much appreciated.
PS: I cannot change the replaceContentView approach for launching new screen, since that has been used at numerous other places and this is the only scenario in which I need to call startActivityForResult
Converting comments as answer,
Try using onResume() to update UI of SummerCostScreen
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)
In my Android program, in the main activity (ReadLog), I will call another activity (History) like below.
Intent intent = new Intent();
intent.setClass(ReadLog.this, History.class);
startActivity(intent);
And in History activity, I will finish making a View called chart and start to show it like below.
View chart = ...;
setContentView(chart);
But now I hope to show it on main UI. So I add a View on main UI (below some buttons), and hope to show the chart inside of the main UI's View. How can I do it?
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.