Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 429
tpk Avatar asked Aug 23 '16 12:08

tpk


2 Answers

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

like image 143
David Wasser Avatar answered Oct 21 '22 22:10

David Wasser


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.

like image 23
Saurabh Kataria Avatar answered Oct 21 '22 21:10

Saurabh Kataria