关于android启动模式“singleTask”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11668144/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
About android launchmode "singleTask"
提问by cn1h
I read the android developer guide and some articles in internet, I'm still confusing about the singleTask
launchmode
. Lets take an example:
我阅读了 android 开发者指南和互联网上的一些文章,我仍然对singleTask
launchmode
. 让我们举个例子:
User launch the App1, android starts a new task. Assume the App1 creates activities in follow order:
用户启动 App1,android 开始一个新任务。假设 App1 按以下顺序创建活动:
ActivityA -> ActivityB -> ActivityC
That's how task1 looks like.
这就是 task1 的样子。
Then user click the home buttom and choose to launch App2, so task1 goes in background and android start a new task: task2, user does something:
然后用户单击主页按钮并选择启动 App2,因此 task1 进入后台并且 android 开始一个新任务:task2,用户执行以下操作:
ActivityD -> ActivityE
now lets say ActivityE
try to start ActivityB
, and ActivityB
has the launchmode singleTask
.
现在让我们说ActivityE
尝试启动ActivityB
,并ActivityB
拥有启动模式singleTask
。
What I understand is that task1 comes to frontend again and task2 goes to background. And task1 looks now like this:
我的理解是 task1 再次进入前端,而 task2 进入后台。task1 现在看起来像这样:
ActivityA -> ActivityB
Which means:
意思是:
The
ActivityC
will be removed from task1 and ActivityB becomes to the top Activity.If user now click on "Back" button, he will come to
ActivityA
of task1 instead of back toActivityE
of task2
在
ActivityC
将从任务1被移除并ActivityB变得顶端活动。如果现在用户点击“返回”按钮,他会来
ActivityA
独立写作,而不是返回到ActivityE
TASK2的
Am I right?
我对吗?
Thanks
谢谢
采纳答案by Errol Dsilva
The ActivityC will be removed from task1 and ActivityB becomes the top Activity.
Yes, you are Right...
你是对的...
ActivityC will be removed
from i.e. the onDestroy
method of the ActivityC will be called. Hence when the user launches Task 1 again, the ActivityB is shown rather than ActivityC.
ActivityC 将removed
来自即ActivityC的onDestroy
方法将被调用。因此,当用户再次启动任务 1 时,将显示 ActivityB 而不是 ActivityC。
Have created 2 Tasks (Projects) and uploaded the same @ SendSpace. Try it out...
已经创建了 2 个任务(项目)并上传了相同的 @ SendSpace。试试看...
If you look at androids documentationit says
如果您查看 androids文档,它会说
" A "singleTask" activity allows other activities to be part of its task. It's always at the root of its task, but other activities (necessarily "standard" and "singleTop" activities) can be launched into that task."
““singleTask”活动允许其他活动成为其任务的一部分。它始终位于其任务的根部,但其他活动(必须是“标准”和“单顶”活动)可以启动到该任务中。
This means that when you click the home button all the activities above the single-task activity (which in your case is ActivityB) are removed from the stack.
这意味着当您单击主页按钮时,单任务活动(在您的情况下为 ActivityB)上方的所有活动都将从堆栈中删除。
In the sample, the app's I had given you earlier if you just run the project "AndroidTest" and click the home button in the logs you can see that the 2nd Activity is put on Pause, and when you launch it again from the "Recent App's" list the 2nd Activity is Destroyed.
在示例中,如果您只是运行项目“AndroidTest”并单击日志中的主页按钮,我之前给您的应用程序您可以看到第二个活动处于暂停状态,当您从“最近应用程序”列出了第二个活动被破坏。
In a scenario where the Activity's above the Single Instance activities (ActivityB) are not removed from the Back Stack, and another application request this Activity (ActivityB) it may not be shown and the intent may be dropped. But this has extremely fewer chances of happening because the user will have to press the Home button and but the current Task\App in the BackStack before he could navigate to another Task\App.
在单实例活动 (ActivityB) 上方的活动未从后台堆栈中移除,并且另一个应用程序请求此活动 (ActivityB) 的情况下,它可能不会显示,并且意图可能会被删除。但是这种情况发生的可能性极小,因为用户必须先按下 Home 按钮,然后才能导航到另一个 Task\App 中的 BackStack 中的当前 Task\App。
Hence the warning
因此警告
The other modes — singleTask and singleInstance — are not appropriate for most applications, since they result in an interaction model that is likely to be unfamiliar to users and is very different from most other applications.
其他模式 — singleTask 和 singleInstance — 不适用于大多数应用程序,因为它们导致用户可能不熟悉并且与大多数其他应用程序非常不同的交互模型。
I hope this solves your doubts.
我希望这能解决你的疑惑。
回答by Blundell
You sound right.
你听起来是对的。
Why don't you test it.
你为什么不测试它。
There is also this app that can help explain launch mode:
还有这个应用程序可以帮助解释启动模式:
https://play.google.com/store/apps/details?id=com.novoda.demos.activitylaunchmode
https://play.google.com/store/apps/details?id=com.novoda.demos.activitylaunchmode
Sources are at https://github.com/gnorsilva/Activities-LaunchMode-demo