把活动使用FLAG_ACTIVITY_REORDER_TO_FRONT前FLAG_ACTIVITY_REORDER_TO_FRONT

2023-09-06 04:12:11 作者:命中不能缺你

我的活动堆栈为A1乙A2,使用A2在顶部。

My activity stack is A1 B A2, with A2 at the top.

A1和A2是现在,在A2中,我想A2至退出并带来A1至前相同活性的实例,A,所以最终栈应该乙A1。我应该如何实现呢?

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?

我目前的code表示A2 executs是:

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

以上code带到B至前面,所以最终的堆栈A1 B,这不是我所期待。

The above code brought B to front, so the final stack is A1 B, which is not what I expected.

你知道吗?

感谢。

推荐答案

这是不可能做你想做的使用意图的标志。

It isn't possible to do what you want using Intent flags.

究其原因是由于 FLAG_ACTIVITY_REORDER_TO_FRONT 的工作方式。当您使用此标志,Android的查找所需活动的一个实​​例,在活动堆栈,从堆栈和扫描的前部开始,直到它到达根/回堆栈。一旦发现指定活动的一个实​​例,它带来了一个正面(即:如果在指定活动的多个实例,它会带来正面的最新的实例)。

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)

在试图重新排序的活动为您,安卓发现实例A2第一和重新排列,为任务的前面。当然,它已经在任务面前,让这个并没有真正做任何事情。

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.

当然,你已经叫完成()在此活动中,你已经尝试过(使用 FLAG_ACTIVITY_ preVIOUS_IS_TOP )来告诉机器人,它不应该考虑当前的活动,而决定做什么,但是这是所有被忽略。安卓认为A2的活性A的最近的实例,并将其重新排列到前面。然后A2被完成和活动B成为任务的前部。用户看到B和活动堆栈:

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)

你需要找到另一种方式来达到理想的效果(因为这个职位几乎是2岁的我假设你已经发现了另一种方式)。

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