从另一个应用程序启动时,应用程序将失去记住其堆栈的能力
现在我已经对此进行了更多研究,我正在重写它以使其更加清晰。如果您正在寻找更多信息,可以在旧版本中找到一些信息。
发生了什么:(
这是指未设置任何 launchMode 的应用程序 设置,使用默认值也是如此)
您从市场或安装程序启动应用程序。这 启动应用程序的根/主要活动 FLAG_ACTIVITY_NEW_TASK 标志并且没有类别。现在的 应用程序堆栈是 [ A ]
然后您将继续执行应用程序中的下一个活动。现在的 此任务中的堆栈是 [ A > B]
然后按主页键,然后重新启动同一应用程序 通过按主屏幕或应用程序托盘中的图标。
此时预计将显示活动 B,因为 那就是你离开的地方。然而,显示了 A 并且任务堆栈是 [A> B> A ] A 的第二个实例是通过 以下标志:FLAG_ACTIVITY_NEW_TASK, FLAG_ACTIVITY_RESET_IF_NEEDED 和 FLAG_ACTIVITY_BROUGHT_TO_FRONT。它 还有 android.intent.category.LAUNCHER 类别。
此时,如果您按返回键,它将返回到 B,因为它 当你离开它的时候。
查看文档似乎 FLAG_ACTIVITY_BROUGHT_TO_FRONT 只能为以下活动设置: 使用 singleTask 或 singleTop 启动模式。然而,这 应用程序尚未设置任何启动模式,因此正在使用 默认标准启动模式。
我认为在这种情况下这不会发生?
我还应该注意,一旦进入这种奇怪的状态,每次从主屏幕或应用程序托盘启动应用程序时都会发生这种情况。如果任务完成(重新启动 电话,强制停止应用程序,或一路回击 stack)将修复此问题,并且将不再错误地启动它。 仅当您从安装程序或市场启动应用程序并且 然后尝试从启动器启动它。
综上所述,为什么会发生这种情况?有办法预防吗?
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.
What is happening:
(This refers to an application that has not set any launchMode
settings and so is using the defaults)
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 ]Then you proceed to the next activity in the application. Now the
stack in this task is [ A > B ]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.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.
At this point, if you hit the back key, it will return you to B, as it
was when you left it.
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.
So in summary, why is this happening? Is there a way to prevent it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是我迄今为止想出的解决方法。我见过的其他一些解决方法涉及查看当前正在运行的任务。然而,我真的不想仅仅为了解决问题而向用户请求另一个许可(GET_TASKS)。
如果您发现其中有任何漏洞,请告诉我。
在主/根活动的 onCreate 方法中,检查意图是否已
FLAG_ACTIVITY_BROUGHT_TO_FRONT 设置,如果是,则调用 finish()。这
然后将 A 的额外实例从堆栈中弹出 [ A > B> ] 变为
[A> B] 从用户的角度来看,它进入了
他们期待的活动。
到目前为止,它似乎在我所有的测试中都有效。我唯一担心的是如果
有一些奇怪的情况,某人的启动器总是会标记
使用 FLAG_ACTIVITY_BROUGHT_TO_FRONT 启动,即使应用程序不是
已经在执行一项任务,因此会将他们完全锁定
因为它会调用 finish() 并且堆栈中没有任何内容
返回到.
--
根据此处评论中的要求,您可以如何检查意图是否为特定标志:
--
另外我应该注意,在进行此修复后,我仍然看到有时会出现此问题。这似乎不是一个完美的解决方案。
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.
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.
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:
--
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.
重写 onConfigurationChanged() 应该可以帮助您保留状态。
http://developer.android.com/reference/android/app/Activity。 html
Overriding of onConfigurationChanged() should help you to retain the state.
http://developer.android.com/reference/android/app/Activity.html