简体   繁体   English

关于android启动模式“singleTask”

[英]About android launchmode “singleTask”

I read the android developer guide and some articles in internet, I'm still confusing about the singleTask launchmode .我阅读了 android developer guide 和互联网上的一些文章,我仍然对singleTask launchmode感到困惑。 Lets take an example:让我们举个例子:

User launch the App1, android starts a new task.用户启动 App1,android 开始一个新任务。 Assume the App1 creates activities in follow order:假设 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.我的理解是 task1 再次进入前端,而 task2 进入后台。 And task1 looks now like this: task1 现在看起来像这样:

ActivityA -> ActivityB

Which means:意思是:

  1. The ActivityC will be removed from task1 and ActivityB becomes to the top Activity. ActivityC将从 task1 中删除,而 ActivityB 将成为顶部 Activity。

  2. If user now click on "Back" button, he will come to ActivityA of task1 instead of back to ActivityE of task2如果用户现在点击“返回”按钮,他将来到 task1 的ActivityA而不是返回到 task2 的ActivityE

Am I right?我对吗?

Thanks谢谢

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来源位于https://github.com/gnorsilva/Activities-LaunchMode-demo

The ActivityC will be removed from task1 and ActivityB becomes the top Activity.

Yes, you are Right...是的,你是对的...

ActivityC will be removed from ie the onDestroy method of the ActivityC will be called. ActivityC 将被removed ,即 ActivityC 的onDestroy方法将被调用。 Hence when the user launches Task 1 again, the ActivityB is shown rather than ActivityC.因此,当用户再次启动任务 1 时,将显示 ActivityB 而不是 ActivityC。

Have created 2 Tasks (Projects) and uploaded the same @ SendSpace .已经创建了 2 个任务(项目)并上传了相同的 @ SendSpace Try it out...试试看...

If you look at androids documentation it 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.在单实例活动 (ActivityB) 上方的活动未从后台堆栈中移除,并且另一个应用程序请求此活动 (ActivityB) 的情况下,它可能不会显示,并且意图可能会被删除。 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.但是这种情况发生的可能性极小,因为用户必须先按下 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.我希望这能解决你的疑惑。

Correct, whenever singleTask activity is launched it comes foreground clearing all activities currently present above it, if your singleTask activity is on the top, it will behave same as singleTop.正确,无论何时启动 singleTask 活动,它都会在前台清除当前存在于其上方的所有活动,如果您的 singleTask 活动位于顶部,则它的行为与 singleTop 相同。

PS - onCreate is not called 2nd time, instead onNewIntent is called. PS - onCreate 不是第二次调用,而是 onNewIntent 被调用。

check this link, very well explained about launchmodes.检查此链接,很好地解释了启动模式。 https://medium.com/android-news/android-activity-launch-mode-e0df1aa72242 https://medium.com/android-news/android-activity-launch-mode-e0df1aa72242

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM