Intent.FLAG_ACTIVITY_CLEAR_TASK和Intent.FLAG_ACTIVITY_TASK_ON_HOME的区别区别、Intent、FLAG_ACTIVITY_CLEAR_TA

2023-09-04 03:30:09 作者:伤感昵称

从Android文档:

From the Android documentation:

FLAG_ACTIVITY_CLEAR_TASK

FLAG_ACTIVITY_CLEAR_TASK

如果传递给Context.startActivity(一个Intent设置),这个标志会   会导致将与活动相关联的任何现有任务   活动开始前被清除。即,该活动   成为人少的任务,新的根,和所有旧   活动结束。这只能用于结合   FLAG_ACTIVITY_NEW_TASK。

If set in an Intent passed to Context.startActivity(), this flag will cause any existing task that would be associated with the activity to be cleared before the activity is started. That is, the activity becomes the new root of an otherwise empty task, and any old activities are finished. This can only be used in conjunction with FLAG_ACTIVITY_NEW_TASK.

FLAG_ACTIVITY_TASK_ON_HOME

FLAG_ACTIVITY_TASK_ON_HOME

如果传递给Context.startActivity(一个Intent设置),这个标志会   引起新启动任务被放置在当前家庭​​的顶   活动任务(如果有的话)。即,pressing从任务背面   总是返回用户家里,即使这是不是最后   活动他们看到。这只能用于结合   FLAG_ACTIVITY_NEW_TASK。

If set in an Intent passed to Context.startActivity(), this flag will cause a newly launching task to be placed on top of the current home activity task (if there is one). That is, pressing back from the task will always return the user to home even if that was not the last activity they saw. This can only be used in conjunction with FLAG_ACTIVITY_NEW_TASK.

考虑这两个片段:

Intent intent = new Intent(this, Activity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);

Intent intent = new Intent(this, Activity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_TASK_ON_HOME);
startActivity(intent);

的办法,我明白了,无论是code段,我们创建一个新的任务,刚刚活动在里面,当你preSS背你去到主屏幕。假设我可能错了,什么是两者之间的区别是什么?另外,如果我有另外的片段与 Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_TASK_ON_HOME | Intent.FLAG_ACTIVITY_CLEAR_TASK 如何将其行为?

推荐答案

有2个片段之间的差异。下面是一些重要的背景信息:

There is a difference between the 2 snippets. Here's some important background information:

android开发中如何设置主界面,比如我有两个activity,如何选择我想要的最先出现

一个任务包含了一叠活动。任务可以在前台或后台。

A task contains a stack of activities. A task can be in the foreground or in the background.

任务也被堆积。如果您在工作 A ,你就会开始一个新的任务 B ,任务 B 堆放在任务 A 的顶部。如果用户presses BACK键足够的时间在任务 B ,他最终会回到任务`A.这是标准的Andr​​oid行为。

Tasks are also "stacked". If you are in task A and you start a new task B, task B is stacked on top of task A. If the user presses the BACK key enough times in task B, he will eventually end up back in task `A. This is standard Android behaviour.

您片段...

Intent intent = new Intent(this, Activity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);

...会做1以下的事情,这取决于...

...will do 1 of the following things, depending...

如果活动具有相同的任务亲和力当前任务(即:从该code正在执行的任务),它将清除当前任务(完成任务的所有活动),并启动活动的新实例到当前的任务。如果用户presses BACK键,这将完成活动,也完成当前任务(因为在任务只有1个活动),并返回用户无论是在主屏幕或开始这任务(也就是该任务的任务堆栈下方的任务)的任务。 如果活动具有比当前任务不同的任务亲和力,且已存在与任务亲和力现有任务(即:现有任务活动将属于),那么现有的任务带到前台,清除(在任务中的所有活动都完成),活动的新实例是在任务的根目录下创建这个任务放在当前任务之上(这样当活动完成后,用户将被丢弃回当前的任务)。 如果活动具有比当前任务不同的任务亲和力,并没有完成这项任务的亲和力,创建一个新的任务和新的实例没有现有的任务活动是在任务的根目录下创建这个任务放在当前任务之上(这样当活动完成后,该用户被丢弃回当前任务)。 If Activity has the same task affinity as the current task (ie: the task from which this code is executing), it will clear the current task (finish all activities in the task) and launch a new instance of Activity into the current task. If the user presses the BACK key, this will finish Activity and also finish the current task (since there is only 1 activity in the task) and return the user to either the HOME screen or the task that started this task (the task that is underneath this task in the task stack). If Activity has a different task affinity than the current task, and there is already an existing task with that task affinity (ie: an existing task that Activity would belong to), then that existing task is brought to the foreground, cleared (all activities in the task are finished), a new instance of Activity is created at the root of the task and this task is put on top of the current task (so that when Activity finishes, the user is dropped back into the current task). If Activity has a different task affinity than the current task, and there is no existing task with that task affinity, a new task is created and a new instance of Activity is created at the root of the task and this task is put on top of the current task (so that when Activity finishes, the user is dropped back into the current task).

这code片段...

This code snippet...

Intent intent = new Intent(this, Activity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_TASK_ON_HOME);
startActivity(intent);

...会做1根据下面的事情...

...will do 1 of the following things depending...

如果活动具有相同的任务亲和力当前任务(即:任务从这个code正在执行)和活动是当前任务的根系活力,这不会做任何事情。它不会开始一个新的任务,它不会清除任何活动,它不会产生活动的新实例,也不会改变时会发生什么的行为当前任务结束。(即:如果当前的任务是由另一个任务开始,当在当前任务的所有活动完成后,其将丢弃该用户返回到在任务堆栈中的previous任务) 如果活动具有相同的任务亲和力当前任务(即:任务从这个code正在执行)和活动是不可以当前任务的根系活力,这将只需创建活动的新实例,并把它放在在当前任务的当前活动的顶部。它不会启动新的任务,它将不清除任何活动,也不会改变的,当当前任务结束会发生什么情况的行为(即:如果当前的任务是由另一个任务,在目前,当所有的活动开始任务完成后,它会删除用户返回到任务堆栈中的previous任务)。 如果活动具有比当前任务不同的任务亲和力,且已存在与任务亲和力现有任务(即:现有任务活动将属于)和活动是现有任务的根系活力,那么现有的任务带到前台和任务从任务堆栈分离(即:如果在任务中的所有活动完成后,它会将用户返回到主屏幕和不可以到开始该任务的任务)。 如果活动具有比当前任务不同的任务亲和力,且已存在与任务亲和力现有任务(即:现有任务活动将属于)和活动是不可以现有任务的根系活力,那么现有任务被带到前台和任务,从任务堆栈分离(即:如果在任务中的所有活动完成后,它会将用户返回到主屏幕和不可以到启动的任务任务)和活动创建并把在该任务中的任何现有活动前的一个新的实例。 如果活动具有比当前任务不同的任务亲和力,并没有完成这项任务的亲和力,创建一个新的任务和新的实例没有现有的任务活动是在任务和新任务的根目录下创建由任务堆栈分离(所以,当活动完成,用户返回到主屏幕和不可以来启动它的任务)。 If Activity has the same task affinity as the current task (ie: the task from which this code is executing) and Activity is the root activity of the current task, this will do nothing. It will not start a new task, it will not clear any activities, it will not create a new instance of Activity, and it will not change the behaviour of what happens when the current task is finished (ie: if the current task was started by another task, when all activities in the current task are finished, it will drop the user back into the previous task in the task stack). If Activity has the same task affinity as the current task (ie: the task from which this code is executing) and Activity is not the root activity of the current task, this will simply create a new instance of Activity and put it on top of the current activity in the current task. It will not start a new task, it will not clear any activities, and it will not change the behaviour of what happens when the current task is finished (ie: if the current task was started by another task, when all activities in the current task are finished, it will drop the user back into the previous task in the task stack). If Activity has a different task affinity than the current task, and there is already an existing task with that task affinity (ie: an existing task that Activity would belong to) and Activity is the root activity of that existing task, then that existing task is brought to the foreground and that task is decoupled from the task stack (ie: when all activities in that task are finished, it will return the user to the HOME screen and not to the task that started that task). If Activity has a different task affinity than the current task, and there is already an existing task with that task affinity (ie: an existing task that Activity would belong to) and Activity is not the root activity of that existing task, then that existing task is brought to the foreground and that task is decoupled from the task stack (ie: when all activities in that task are finished, it will return the user to the HOME screen and not to the task that started that task) and a new instance of Activity is created and put on top of any existing activities in that task. If Activity has a different task affinity than the current task, and there is no existing task with that task affinity, a new task is created and a new instance of Activity is created at the root of the task and the new task is decoupled from the task stack (so that when Activity finishes, the user is returned to the HOME screen and not to the task that started it).

最后,这段代码...

and finally, this snippet...

Intent intent = new Intent(this, Activity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_TASK_ON_HOME);
startActivity(intent);

...会做1以下的事情,这取决于...

...will do 1 of the following things, depending...

如果活动具有相同的任务亲和力当前任务(即:从该code正在执行的任务),它将清除当前任务(完成任务的所有活动),并启动活动的新实例到当前的任务。如果用户presses BACK键,这将完成活动,也完成当前任务(因为在任务只有1个活动),并返回用户HOME画面。 如果活动具有比当前任务不同的任务亲和力,且已存在与任务亲和力现有任务(即:现有任务活动将属于),那么现有的任务带到前台,清除(在任务中的所有活动都完成),活动的新实例是在任务的根目录下创建这个任务从任务堆栈分离(所以,当活动完成后,用户将返回到主屏幕) 如果活动具有比当前任务不同的任务亲和力,并没有完成这项任务的亲和力,创建一个新的任务和新的实例没有现有的任务活动是在任务的根目录下创建这个任务从任务堆栈分离(所以,当活动完成后,用户返回到主屏幕)。 If Activity has the same task affinity as the current task (ie: the task from which this code is executing), it will clear the current task (finish all activities in the task) and launch a new instance of Activity into the current task. If the user presses the BACK key, this will finish Activity and also finish the current task (since there is only 1 activity in the task) and return the user to the HOME screen. If Activity has a different task affinity than the current task, and there is already an existing task with that task affinity (ie: an existing task that Activity would belong to), then that existing task is brought to the foreground, cleared (all activities in the task are finished), a new instance of Activity is created at the root of the task and this task is decoupled from the task stack (so that when Activity finishes, the user is returned to the HOME screen). If Activity has a different task affinity than the current task, and there is no existing task with that task affinity, a new task is created and a new instance of Activity is created at the root of the task and this task is decoupled from the task stack (so that when Activity finishes, the user is returned to the HOME screen).

我知道这答案是长期和复杂的,但也有只是这么多不同的情况下。我可能还没有覆盖所有可能的情况下(例如,如果活动有一个特殊的发射模式)...

I realize that this answer is long and complicated, but there are just so many different cases. I probably haven't even covered all the possible cases (for example, if Activity has a special launch mode)...