Traverse between activities in stack - android

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.

Related

Activity back tracking in Activity stack - Android

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.

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

starting existing android activity

hi i have an application
A is launcher activity from that i create B activity. From B i want to go back to A without deleting the instance of B(so no finish) then i want to create a new instance of B lets call it B2. And from A i want to be able to show B or B2 without recreating them.
again i stress that B,B2 are the same class just diffrent instances.
from B when i want to go to A i thought of using
Intent myIntent = new Intent(getBaseContext(), MainAct.class);
myIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(myIntent);
but i think that its a mistake because it will clear other instances.
what intent must i use in the cases?
in c# windows application we would do B.hide();A.show(). then from A we could do
A.hide(); B.SHOW() or B1.show();
how can i do that in android please?
If B is an Activity, you can't have two different instances of it.
You can have 2 different Activities that extend the same class, but you've indicated you don't want that.
You need to find a way to change the appearance and behavior of B rather than creating a new instance.
Please consider reading about the Activity Lifecycle.
use FLAG_ACTIVITY_REORDER_TO_FRONT
from the doc
If set in an Intent passed to Context.startActivity(), this flag will
cause the launched activity to be brought to the front of its task's
history stack if it is already running.

Get activity from Back Stack keeping history, instead of creating new one

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.

android: Activity.startActivityForResult() or ActivityGroup?

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.

Categories

Resources