Using multiple instances of startActivityForResult through a linked chain of Activities - android

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.

Related

Traverse between activities in stack

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.

Android 3 Activitys - how to hande passing data?

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.

Replace a fragment from other activity than the host

I have a MainActivity with a container FrameLayout in which I change multiple Fragments (Fragment A, Fragment B etc).
In one of this fragments let's say Fragment A I have to open another activity (Activity X).
The problem is that from this activity when I press a button I have to change Fragment A with Fragment B (in the background somehow) and after that, slideout Activity X (with translate animation), and slidein Fragment B ,all this without restarting the MainActivity because I have to keep the state.
How can I do this?
Thanks
Android uses loosely coupled components as its main building blocks. As you know, Activities are one of the main Android building blocks. Thus, interacting between activities are very restricted to a few ways.
Passing data via Intents by startActivity(), startActivityForResult() etc. This way is useful whenever you are starting new activities.
Sending broadcast Intents. This could be useful once you want to send a signal to your another app's component.
Utilizing shared Application object.
Java static fields and some other ways.
In your case I would recommend you to use a Dialog Fragment instead of your second activity, if your second activity is just a login activity or something like that.
UPDATE #1:
If you really would like to keep your second activity, so I would personally recommend using local broadcast mechanism.
Also there are another way to get this done. You could start your second activity as startActivityForResult and then whenever user gets back from your second activity to your first one, your first activity can get informed by its onActivityResult method. There you could switch those fragments.

custom object between activities

I am trying to implement a three step process via 3 activities.
Activity A can proceed to Activity B
Activity B can proceed to Activity C, can return to previous Activity A
Activity C can return to Activity B
I have a custom object which is used to retained all the variables across A, B, C. When I proceed from A to B, I just pass it through the intent.putExtra(), likewise for B to C.
When I return from B to A or C to B, changes are lost. I use finish() to return to previous in current implementation.
I also need to take care of the back button in that no changes are lost.
How can I implement such behaviour:
navigate between A <--> B <--> C
and retaining the changes into the object? (page loads with information from object in onCreate()
you can override onResume() function of the activity and call those changing methods there.

Android three activities, pass data from third activity to parent activity

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

Categories

Resources