mrtpk
mrtpk

Reputation: 1437

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.

Upvotes: 5

Views: 409

Answers (2)

David Wasser
David Wasser

Reputation: 95578

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):

Upvotes: 5

Saurabh Kataria
Saurabh Kataria

Reputation: 81

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.

Upvotes: 2

Related Questions