I have 3 activities. Let's say A, B, C. Main activity is A.
I'd like to start B from A then start C from B, then close B when C opens. Finally upon closing of C pass data to A.
How's this possible especially with Intent and startActivityForResult? Is it at all possible with the two latter methods?
Thanks.
Supose you are using a button, when you click the button to start the activity C, you can finish the activity B, and get the action in the activity A.
Use onActivityResult() on activity A. On this pont, you can start the activity C using startActivityForResult().
This way, when you finish C, you can pass data to A, again using onActivityResult().
Renan Lopes Ferreira's reply did the trick. Cheers.
You can always start B from A and C from B using Intents.
As you are removing A from the stack, C cannot directly pass data to A using startActivityforResult() and onActivityResult). (Isn't it obvious, A doesn't even exist in memory).
So for A to access C's data, you must store C's data somewhere, so that A can access it, the next time it is up. Now, here the other scenario would be store it as persistent or non-persistent data. If you want the data to be persistent, it is recommended to use Shared Preferences, Database etc. and if you want it to be non-persistent then you may use Singleton etc.
Refer : http://developer.android.com/guide/faq/framework.html
Related
Suppose I have activity instances A1, B1, A2, B2, C1 of activities A, B, C in stack. How can I traverse to Activity instance B1 from C1?
Let us generalize there will be 'n' number of activities between B1 and C1.
I don't want to create a new instance of B from C1.
This is actually a very bad architecture for Android. If you create multiple instances on an Activity, there is no way to specifically address them, for example: "Go back to the first instance of ActivityB". Android isn't designed like this.
You should avoid creating multiple instances of an Activity. It is beter to use the same instance and just create the "illusion" of multiple instances by swapping out the underlying data and maybe adding a state transition on the display so that it looks like you are starting another Activity.
Another possible solution would be to use a lot of startActivityForResult() and return information to the calling Axctivity about what to do next.
For more details see (even though these questions are specifically about using FLAG_ACTIVITY_REORDER_TO_FRONT, the problem is still basically the same):
Managing Android Activity stack: bring specific activity instance to front
Multiple activity instances and FLAG_ACTIVITY_REORDER_TO_FRONT
Bring an activity to front using FLAG_ACTIVITY_REORDER_TO_FRONT
Use Flags with Intent.
official Docs: https://developer.android.com/guide/components/tasks-and-back-stack.html
pass the flag along with Intent
FLAG_ACTIVITY_CLEAR_TOP
also you can paas multiple FLags in a single Intent according to your need.
Hope this helps.
I have 4 Activities.
Activity A is where I want multiple types of custom objects to end up for processing.
I used one instance of startActivityForResult to connect from Activity A to Activity B, and from there I use FLAG_ACTIVITY_FORWARD_RESULT when I connect to Activity C. I call this one more time to connect to Activity D.
I can get any of the custom objects back to Activity A easily using the setResult(), my problem is I want to be able to get a Custom Object from Activity D, back to Activity C. Can I use startActivityForResult again from C to D, and when I call setResult on Activity D, will it point back to Activity C or pass down the line due to FLAG_ACTIVITY_FORWARD_RESULT?
Consider to use one host Activity as a navigation/task controller and multiple Fragments with callback results.
If you want to stick to Activities move all navigation logic to the root activity.
So I just want to clarify the life cycles a little. I have three activitys A(main), B and C. B is startet from A with some extra infromation, that it needs to show the correct content. Now B starts the Activity C (no extra content needed).
Can now (Activity C is in foreground) activity B be killed? If so, when pressing the back button, do I need to transfer that same infromation from C -> B, that was transfered at the first creation of B? Basically, what I wnat to know is, if extra content is used to start activity from parent activity, should this same extra content be used to start the activity from its child?
Thank you
Jaka
No need to pass back data from C -> B when you press back button.
On B's onCreate, store the additional data into private class variables and you can use it when the activity is restored.
Is it possible to go back from Activity B to Activity A without killing Activity B so that you can go back to B again without creating a new one?
Call startActivity() with an Intent that points to A. By default, this will create a fresh instance of A; if that's not what you want, add FLAG_ACTIVITY_REORDER_TO_FRONT to the Intent.
From the user's standpoint, though, you're likely to confuse them, as this will scramble the back stack. I would recommend that you find some other solution for whatever problem you are perceiving (e.g., use a better process-level cache, if your problem is loading data).
I have an activity that is started from the search api. I would like it to "return" some values to the activity that was running when the search was instigated, in a similar way to startActivityForResult, but I can't see how to do it. Any suggestions?
Activity A -> (startActivity) ->
Activity B -> (Search) -> Activity C
-> (return) -> Activity B
At the moment I'm starting a new instance of activity B using the values. This works, but the activity stack is then not the way it should be. Ie, pressing back from B would go back via C, B, A, instead of just A.
Any suggestions on the way to do this?
I'd like to know how to do this too. The only kludge I can think of is to have public static variables in B to pass the result back in, and then have C call finish() after setting said variables, with B processing the results in the onResume() method.
I've come up with a solution: to combine B and C. Instead of having a separate search handling activity, I can get B to handle the search and pop up a dialog with the results in it. It doesn't feel elegant, I'd much rather have the separate, but it's the best solution I could find. This is assuming, of course, that it works, but I can't currently see a reason for it not to (in my particular situation).
EDIT: Here's the code. EditLocation is A, GetLocationMap is B
https://github.com/spookypeanut/Wake-Me-At/tree/e0dde4153c375c20bec3d7201b4faac2300f5956