应用程序失去了它的能力,要记住它的堆栈从另一个应用程序启动时,它的、应用程序、堆栈、启动时

2023-09-12 04:34:11 作者:爷丶毒霸 后宫

现在,我已经研究这个更我重写这使其更清晰。如果您正在寻找更多的信息,有中老年编辑一些可用的。

Now that I have researched this even more I am rewriting this to make it clearer. If you are looking for more info, there is some available in older edits.

这是怎么回事:

(这是指还没有设置任何launchMode一个应用 设置等使用默认值)

(This refers to an application that has not set any launchMode settings and so is using the defaults)

您启动一个应用程序从市场还是从安装程序。本 启动的应用程序的根目录/主要活动 FLAG_ACTIVITY_NEW_TASK标志,无类别。眼下 应用程序堆栈[A] ​​

You launch an app from the Market or from the Installer. This launches the root/main activity of the application with the FLAG_ACTIVITY_NEW_TASK flag and no categories. Right now the applications stack is [ A ]

这样就可以进行应用程序的下一个活动。现在, 堆在这项任务是[A> B]

Then you proceed to the next activity in the application. Now the stack in this task is [ A > B ]

然后你preSS的home键,然后重新启动相同的应用程序 由pressing是在主屏幕或应用程序托盘它的图标。

Then you press the home key and then relaunch the same application by pressing it's icon from either the home screen or the app tray.

什么是预计在这一点上是b活动将显示,自 这是你离开的地方。然而,一个显示和任务堆栈是 [A> B> A] A的第二个实例与启动 以下标志:FLAG_ACTIVITY_NEW_TASK, FLAG_ACTIVITY_RESET_IF_NEEDED和FLAG_ACTIVITY_BROUGHT_TO_FRONT。它 也有android.intent.category.LAUNCHER类别。

What is expected at this point is that activity B will show, since that is where you left off. However A is shown and the tasks stack is [ A > B > A ] This second instance of A is launched with the following flags: FLAG_ACTIVITY_NEW_TASK, FLAG_ACTIVITY_RESET_IF_NEEDED, and FLAG_ACTIVITY_BROUGHT_TO_FRONT. It also has the android.intent.category.LAUNCHER category.

在这一点上,如果你打的返回键,它会返回到B,因为它 当您离开它。

At this point, if you hit the back key, it will return you to B, as it was when you left it.

纵观文档看来,如果 FLAG_ACTIVITY_BROUGHT_TO_FRONT应仅适用于活动中设置 使用singleTask或singleTop launchModes。但是,此 应用程序还没有设置任何launchModes并因此使用 默认标准launchMode。

Looking at the documentation it seems as if FLAG_ACTIVITY_BROUGHT_TO_FRONT should only be set for activities that use the singleTask or singleTop launchModes. However, this application has not set any launchModes and is therefore using the default standard launchMode.

我想这是不是假设发生在这种情况下?

I assume this is not suppose to happen in this case?

我也应该注意,一旦它进入这个奇怪的状态,然后就发生每次应用程序被从主屏幕或应用程序托盘推出。如果任务已结束(在重新启动 电话,强制停止的应用程序,或者打回,一路过关斩将的 堆栈)会解决这个问题,将不再启动它不正确。 如果从安装程序或市场,启动应用程序它只发生 然后尝试从启动启动它。

I should also note, that once it gets into this weird state, then it happens everytime the app is launched from the home screen or app tray. If the task is finished (restarting the phone, force stopping the app, or hitting back all the way through the stack) will fix this issue and will no longer launch it incorrectly. It only happens if you launch the app from the installer or market and then try to launch it from the launcher.

因此​​,在总结,为什么会出现这种情况?有没有一种方法,以prevent呢?

So in summary, why is this happening? Is there a way to prevent it?

推荐答案

下面是一种变通方法我想出了这么远。我见过参与解决一些其他方法着眼于当前正在运行的任务。可是,我真的不想再要问另一种权限(GET_TASKS)从用户只是为了让周围的工作。

Here is a workaround I have come up with so far. Some other workarounds I have seen involved looking at the currently running tasks. However, I really did not want to have to ask for another permission (GET_TASKS) from the user just to make a work around.

请让我知道,如果你看到任何漏洞在此。

Please let me know if you see any holes in this.

在主/根系活力的onCreate方法,检查是否意图有 所述FLAG_ACTIVITY_BROUGHT_TO_FRONT置,如果是,调用finish()。本 那么持久性有机污染物的额外的实例,从堆栈[A> B> A]变 [A> B],并从用户的角度看,它推出成 活动他们期待。

In the main/root activity's onCreate method, check if the intent has the FLAG_ACTIVITY_BROUGHT_TO_FRONT set and if so, call finish(). This then pops the extra instance of A off the stack [ A > B > A ] becomes [ A > B ] and from the users perspective, it launches into the activity they were expecting.

这似乎是我所有的测试工作至今。我唯一​​担心的是,如果 有一些怪异的情况下,某人的发射总是国旗的 与FLAG_ACTIVITY_BROUGHT_TO_FRONT启动,即使该应用程序是不是 已经在一个任务,因此将完全禁止他们出去 因为它会调用光洁度()和在堆栈到没有什么 返回。

It seems to work in all of my tests so far. My only worry is that if there is some weird case where someones launcher would always flag a launch with FLAG_ACTIVITY_BROUGHT_TO_FRONT even if the app wasn't already in a task, and therefore would completely lock them out because it would call finish() and not have anything in the stack to return to.

-

按照要求在这里的评论是如何检查是否意图特定的标志:

As requested in the comments here is how you can check if an intent a specific flag:

int flags = intent.getFlags();
boolean hasFlag = flags & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT == Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT;

-

我也应该注意,我仍然看到这个问题时有发生,在发生此修复程序。这似乎不是一个完美的解决方案。

Also I should note that I am still seeing this problem occur sometimes with this fix in place. It doesn't seem to be a perfect solution.