Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
562 views
in Technique[技术] by (71.8m points)

android - Bring an activity to front using FLAG_ACTIVITY_REORDER_TO_FRONT

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...