I have the following situation:
activity A -> resultA
activity B -> resultB
activity C -> resultC
and I need all the results (A, B & C) to build the final out put?
What's the better way:
AA extends Activity {
startActivityForResult(A);
startActivityForResult(A);
startActivityForResult(A);
}
or to make AA subtype of ActivityGroup?
Activities A, B and C do not require to be visible (foreground) at the same time.
Thank you!
IMHO, none of the above.
Popping out of one activity into another briefly via startActivityForResult() is fine...once. It gets cumbersome for users and developers alike when there are several.
Consider recasting your UI as a wizard, perhaps driven by a ViewFlipper, so you can keep all of this in a single activity. It also provides the user with a recognized pattern for going back and forth within the chain of steps leading to them providing all of the information you need.
Related
I am having two activities, Activity A and activity B.
Activity A starts activity B.
So, the activity stack after some interactions will look like A -> B -> A -> B.
The problem:
I need to go the first activity A in the stack from activity B (last B in the stack). I am using FLAG_CLEAR_TOP as well as Intent.FLAG_ACTIVITY_NEW_TASK for achieving the same.
Right now, the activity A (stack pos 3) will be shown from activity B, but when I press back button, the activity B will be shown again (since activity B (stack pos 2) is already there in the stack).
How do I overcome this issue?
PS: I tried using launchMode singleInstance and singleTask for activities A and B, but that solution doesn't work for my app.
Thanks in advance.
Add finish (); after switching activites like
Intent intent = new Intent (activity_a.this, activity_b.class);
startActivity(intent);
finish ();
Avoid creating multiple instances of activities if possible. Android isn't designed to allow you to identify (and return to) a specific instance of an Activity in the stack, if you have multiple instances of the same Activity in the stack.
Depending on your application, there are different ways to solve the problem.
One way that may work for you is to use <activity-alias>, which allows you to reuse an existing Activity implementation by another name.
Another way would be to create another Activity class, that just inherits from the original, so that you have 2 classes with exactly the same code, but with different names.
The best way is to rearchitect your application so that you only ever have one instance of each Activity alive at any given time. You can do this by rearranging the task stack using Intent.FLAG_ACTIVITY_REORDER_TO_FRONT.
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'm trying to do something like this:
I have some activities in task:
A->B->C
and from C i want to call instance of B from stack and keep current history. So the wondered result looks like this:
A->B->C->B
where B and B is the same instances, but with possibility to navigate back with Back button.
Is it possible?
May be duped:
Android task history stack: can have "duplicate activities"?
As I know Android doesn't support this case.
You can have either:
A->B->C->B (where B are both activities of the same class, but which are two different objects)
pr
A->C->B (where B is brought to the front).
However, I think you can build something on your own. If you will serialize the state of B on onPause() and deserialize it on onCreate() and onResume(), you may have two B's in the stack (which will be separate objects), but they still have the same state (as example all members).
I solved my problem by realising my own Back Stack. Main idea was to use FLAG_ACTIVITY_BRING_TO_TOP for indicating my singleton acivities.
I intend to start 3 activities in a chain (like from main open Activities A, B and then C, which will be visible for the user), but I wasn't able to find some way how to do that in Android. Do not ask me why, I just have to do that for restoring my application state, where is was before.
Thanks for any ideas
Waypoint
Edit:
Ok, I have tried opening activities in For cycle, but they aren't opened properly. They are chained, but recreated only when I press back button and they display to me. I need some solution which leads to: open A, if A is opened check if needs to open B -> YES, open B, check if needs to open C -> YES, open C, no need to open another activity -> FINISH
prior to start any activity , decide which activity should be start .
Lets take your case >>first check for B , if yes check for C , now open the required 1 .
i understand comparable data is inside the activities, but a right data structure will always allow you to access the data wherever and whenever it actually requires .
For future readers: if you want to start activity with proper back stack, you should use TaskStackBuilder.
define the natural hierarchy for your activities by adding the android:parentActivityName attribute to each element in your app manifest file
create an instance of TaskStackBuilder and call addNextIntentWithParentStack(), passing it the Intent for the activity you want to start.
For more details see official documentation
Override the onResume-method in each activity. Add the check and the start of the activity there.
public void onResume() {
if( condition )
startActivity( intentForTheNextActivity );
}
Where condition is whatever condition you might have (in your example if B should be started, C should be started etc.) and intentForTheNextActivity is the intent for the following activity in the chain (e.g. if now in A, the intent is for B etc.).
I'm having a very hard time understanding exactly what it is you're trying to do. Sometimes it seems like it's a chain (A opens B, B opens C and so forth) sometimes it seems you want some random flow (A opens B, B opens A, A opens B, B opens C) - which makes it really hard to give you a specific answer.
What I can do, is recommend that you read up on the following:
Activity Lifecycle
Starting Activities and getting results (in particular the methods startActivityForResult and setResult)
If you need more help than this - you need to explain yourself better (maybe with a diagram or some sample code of what you have tried so far).
You didn't provide any information of what you've tried, so i'll give you the simplest answer:
method startActivity(Intent intent), more info here.
Edit: Hmm, how about this? I don't have SDK around me now, but i can provide a concept. I'm not sure if it works, but i hope it'll guide you to soultion.
Let's imagine this is ActivityA's code:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (doINeedActivityB) {
Intent activityC = new Intent(this, ActivityC.class);
this.startActivity(activityA);
}
if (doINeedActivityC) {
Intent activityC = new Intent(this, ActivityC.class);
this.startActivity(activityC);
}
}
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