My activity stack is A1 B A2, with A2 at the top.
A1 and A2 are instances of the same activity, A. Now in A2, I want A2 to exit and bring A1 to front, so the final stack should be B A1. How should I implement it?
My current code that A2 executs is:
finish();
intent = new Intent(this, A.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP);
startActivity(intent);
The above code brought B to front, so the final stack is A1 B, which is not what I expected.
Any idea?
Thanks.
It isn't possible to do what you want using Intent flags.
The reason is due to the way that FLAG_ACTIVITY_REORDER_TO_FRONT
works. When you use this flag, Android looks for an instance of the desired activity in your activity stack, starting from the front of the stack and scanning until it gets to the root/back of the stack. As soon as it finds an instance of the specified activity, it brings that one to the front (ie: if there are multiple instances of the specified activity it will bring to the front the most recent instance).
In your case, the activity stack looks like:
A1, B, A2 (front of task)
When trying to reorder your activity A, Android finds the instance A2 first and reorders that to the front of the task. Of course, it was already at the front of the task so this doesn't really do anything.
Of course you've already called finish()
on this activity and you've tried (by using FLAG_ACTIVITY_PREVIOUS_IS_TOP
) to tell Android that it shouldn't consider the current activity while deciding what to do, but this is all ignored. Android sees A2 as the most recent instance of activity A and reorders it to the front. Then A2 is finished and activity B becomes the front of the task. The user sees "B" and the activity stack is:
A1, B (front of task)
You'll need to find another way to achieve the desired results (since this post is almost 2 years old I assume that you've already found another way).
In my app, I have the exact same situation.
My solution was to create a invisible Start-up activity, X, which redirects to A
So your stack would look like this:
X A1 B A2
At any point I need to get back to A1, I rewind to X (using FLAG_ACIVITY_CLEAR_TOP), which, in turn will start A.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With