单击应用程序的启动图标会发生什么? [英] What happens when you click on an application's launch icon?

查看:101
本文介绍了单击应用程序的启动图标会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

单击应用程序的启动图标会发生什么情况?

 What happens when you click on an app's launch icon?

  1. 是否总是发送新的意图,或者结果有时与从最近的任务中恢复任务相同?

  1. Is a new intent always sent, or is the result sometimes the same as resuming a task from recent tasks?

如果发送了一个Intent,何时将其发送到新活动实例的onCreate()方法,何时将其通过现有活动的onNewIntent()路由?

If an intent is sent, when does it get sent to the onCreate() method of a new activity instance and when does it get routed through onNewIntent() of an existing activity?

让我们假设意图是通过任务中现有活动的onNewIntent()路由的.它发送到哪个活动?一个最接近顶部或一个最接近根的位置?它是否总是发送到应用程序启动活动的实例,还是有时可以发送到与根目录具有相同亲和力的活动?它可以发送到与根没有相同亲和力的活动吗?

Let's suppose the intent gets routed through onNewIntent() of an existing activity in the task. Which activity does it get sent to? The one nearest the top or the one nearest the root? Will it always get sent to an instance of the application's launch activity or can it sometimes get sent to an activity with the same affinity as the root? Can it ever get sent to an activity which does not share the same affinity as the root?

最后,这一切如何受到任务中活动的各种启动模式(标准,单顶,单实例,单任务)的影响?

Finally, how is this all affected by the various launch modes (standard, single top, single instance, single task) of the activities in the task?

如果外面有人知道这一切,请帮助我!

If there is anyone out there who understands all this, please help me!

推荐答案

What happens when you click on an app's launch icon?

启动器应用程序以意图[操作= Intent.ACTION_MAIN,类别= Intent.CATEGORY_LAUNCHER和标志= Intent.FLAG_ACTIVITY_NEW_TASK]调用startActivity.

Launcher apps calls startActivity with an intent [action = Intent.ACTION_MAIN, category = Intent.CATEGORY_LAUNCHER and flag = Intent.FLAG_ACTIVITY_NEW_TASK].

关于Intent.FLAG_ACTIVITY_NEW_TASK,来自文档:

Regarding Intent.FLAG_ACTIVITY_NEW_TASK, from docs:

使用此标志时,如果活动的任务已经在运行 您现在开始,则新的活动将不会开始; 而是将当前任务简单地带到 屏幕上显示了它最后一次进入的状态.

When using this flag, if a task is already running for the activity you are now starting, then a new activity will not be started; instead, the current task will simply be brought to the front of the screen with the state it was last in.

onNewIntent基础知识:

onNewIntent.如果活动已将singleTop设置为启动模式,或者意图启动活动已将标志FLAG_ACTIVITY_SINGLE_TOP设置为活动且活动实例已经位于目标任务的顶部,则也将传递此消息.这意味着试图启动一个新的活动实例,而不是现有实例本身需要处理意图.

onNewIntent is delivered only when activity has set either singleTask, singleInstance launch modes. It is also delivered if activity has set singleTop launch mode or the intent to start the activity has set the flag FLAG_ACTIVITY_SINGLE_TOP and the activity instance is already at the top of the target task. It means an attempt was made to launch a new instance of activity, instead the existing instance itself need to handle the intent.

以下是对您的查询的答复:

总是发送新的意图,还是有时结果与 从最近的任务中恢复一个任务?

Is a new intent always sent, or is the result sometimes the same as resuming a task from recent tasks?

如果任务已经在运行,则将其置于前台.如果使用FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET标志启动活动,然后将任务置于前台,则该活动将被终止.来自文档:

If the task is already running, it is brought to foreground. In case FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET flag was used to launch a activity and latter the task is brought to foreground, then the activity is killed. From docs:

这在您逻辑上中断的情况下很有用 应用.例如,电子邮件应用程序可能具有一个命令来 查看附件,该附件将启动要显示的图像查看活动 它.此活动应该是电子邮件应用程序任务的一部分, 因为它是用户参与的任务的一部分.但是,如果 用户离开该任务,然后从家里选择电子邮件应用程序, 我们可能希望他们返回他们正在查看的对话,而不是 图片附件,因为这很令人困惑.通过设置此标志 启动图像查看器,该查看器及其任何活动时 用户下一次返回邮件时,开始将被删除.

This is useful for cases where you have a logical break in your application. For example, an e-mail application may have a command to view an attachment, which launches an image view activity to display it. This activity should be part of the e-mail application's task, since it is a part of the task the user is involved in. However, if the user leaves that task, and later selects the e-mail app from home, we may like them to return to the conversation they were viewing, not the picture attachment, since that is confusing. By setting this flag when launching the image viewer, that viewer and any activities it starts will be removed the next time the user returns to mail.

-

如果发送了一个意图,那么它将何时发送给onCreate()方法 一个新的活动实例以及何时将其路由到 现有活动的onNewIntent()?

If an intent is sent, when does it get sent to the onCreate() method of a new activity instance and when does it get routed through onNewIntent() of an existing activity?

在创建新的活动实例时调用

onCreate.如果已经存在一个活动实例,并且不需要创建新实例,则调用onNewIntent,例如singleInstancesingleTask和有条件的singleTop(如上所述).

onCreate is called while creating a new instance of activity. onNewIntent is called if already an activity instance exists and no new instance need to be created, as in case of singleInstance, singleTask and conditionally singleTop (as described above).

让我们假设意图是通过一个对象的onNewIntent()路由的 任务中的现有活动.它发送到哪个活动?这 最接近根部的一个还是最靠近根部的一个?会永远得到吗 发送到应用程序启动活动的实例还是可以 有时被发送到与根具有相同亲和力的活动? 它可以发送到不共享同一活动的活动吗 亲和力是根?

Let's suppose the intent gets routed through onNewIntent() of an existing activity in the task. Which activity does it get sent to? The one nearest the top or the one nearest the root? Will it always get sent to an instance of the application's launch activity or can it sometimes get sent to an activity with the same affinity as the root? Can it ever get sent to an activity which does not share the same affinity as the root?

对于singleTasksingleInstance,它必须是任务的根目录.对于singleTop,它必须是任务的首要任务.

In case of singleTask and singleInstance it has to be root of the task. In case of singleTop it has to be top activity of the task.

最后,这一切如何受到各种启动模式的影响 (标准,单顶,单实例,单任务)活动 在任务中?

Finally, how is this all affected by the various launch modes (standard, single top, single instance, single task) of the activities in the task?

我希望到目前为止提供的解释能够得到答案.

I hope the explanation provided till now, answers it.

更新1:

这是启动器代码,用于将标志添加到意图:

Here is the Launcher code which adds the flags to intent:

void processShortcut(Intent intent) {
    ....
    Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
    mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
    ....
}

void startActivitySafely(Intent intent) {
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    ...
    startActivity(intent);
}

这篇关于单击应用程序的启动图标会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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