当从另一个应用程序启动时,应用程序失去了记住其堆栈的能力 [英] App loses its ability to remember its stack when launched from another application

查看:32
本文介绍了当从另一个应用程序启动时,应用程序失去了记住其堆栈的能力的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我已经对此进行了更多研究,我正在重写它以使其更清晰.如果您正在寻找更多信息,可以在旧版本中找到一些信息.

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)

  1. 您从市场或安装程序启动应用程序.这启动应用程序的根/主要活动FLAG_ACTIVITY_NEW_TASK 标志并且没有类别.现在的应用程序堆栈是 [ A ]

  1. 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 ]

然后你按下 Home 键,然后重新启动同一个应用程序通过在主屏幕或应用托盘中按下它的图标.

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 并且任务堆栈是[ 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 启动模式.然而,这应用程序没有设置任何启动模式,因此正在使用默认标准启动模式.

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?

我还应该注意,一旦它进入这种奇怪的状态,那么每次从主屏幕或应用程序托盘启动应用程序时都会发生这种情况.如果任务完成(重新启动电话,强制停止应用程序,或一直回击stack) 将解决此问题,并且不会再错误地启动它.仅当您从安装程序或市场启动应用程序并且然后尝试从启动器启动它.

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.

总之,为什么会发生这种情况?有什么办法可以预防吗?

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.

在 main/root Activity 的 onCreate 方法中,检查 Intent 是否有FLAG_ACTIVITY_BROUGHT_TO_FRONT 设置,如果是,则调用 finish().这然后将 A 的额外实例从堆栈中弹出 [ 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 启动,即使应用程序不是已经在一项任务中,因此会将它们完全锁定因为它会调用 finish() 并且堆栈中没有任何内容返回.

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.

这篇关于当从另一个应用程序启动时,应用程序失去了记住其堆栈的能力的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆