Android Intent.FLAG_ACTIVITY_CLEAR_TASK 和 Intent.FLAG_ACTIVITY_TASK_ON_HOME 的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21833402/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:10:14  来源:igfitidea点击:

Difference between Intent.FLAG_ACTIVITY_CLEAR_TASK and Intent.FLAG_ACTIVITY_TASK_ON_HOME

androidandroid-intent

提问by kolistivra

From the Android documentation:

从 Android 文档:

FLAG_ACTIVITY_CLEAR_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_CLEAR_TASK

如果在传递给 Context.startActivity() 的 Intent 中设置,则此标志将导致在启动活动之前清除与活动关联的任何现有任务。也就是说,该活动成为一个原本为空的任务的新根,并且所有旧活动都已完成。这只能与 FLAG_ACTIVITY_NEW_TASK 结合使用。

and

FLAG_ACTIVITY_TASK_ON_HOME

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.

FLAG_ACTIVITY_TASK_ON_HOME

如果在传递给 Context.startActivity() 的 Intent 中设置,则此标志将导致新启动的任务放置在当前主活动任务的顶部(如果有)。也就是说,即使这不是他们看到的最后一个活动,从任务中按回总是会让用户返回家中。这只能与 FLAG_ACTIVITY_NEW_TASK 结合使用。

Consider these two snippets:

考虑这两个片段:

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

and

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

The way I understand, in either code snippet we create a new task which has just Activity in it, and when you press back you go to the home screen. Assuming that I'm probably mistaken, What are the differences between the two? Also, if I had another snippet with Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_TASK_ON_HOME | Intent.FLAG_ACTIVITY_CLEAR_TASKhow would that behave?

根据我的理解,在任一代码片段中,我们都创建了一个只有 Activity 的新任务,当您按下返回键时,您将转到主屏幕。假设我可能弄错了,两者之间有什么区别?另外,如果我有另一个片段Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_TASK_ON_HOME | Intent.FLAG_ACTIVITY_CLEAR_TASK会如何表现?

回答by David Wasser

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

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

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

  • Tasks are also "stacked". If you are in task Aand you start a new task B, task Bis 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.

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

  • 任务也是“堆叠”的。如果您在任务中A并开始一项新任务B,则任务B将堆叠在任务之上A。如果用户在 task 中按下 BACK 键足够多的次数B,他最终会回到 task 'A 中。这是标准的 Android 行为。

Your snippet...

你的片段...

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

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

...将做以下事情之一,具体取决于...

  1. If Activityhas 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 Activityinto the current task. If the user presses the BACK key, this will finish Activityand 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).
  2. If Activityhas a different task affinity than the current task, and there is already an existing task with that task affinity (ie: an existing task that Activitywould belong to), then that existing task is brought to the foreground, cleared (all activities in the task are finished), a new instance of Activityis created at the root of the task and this task is put on top of the current task (so that when Activityfinishes, the user is dropped back into the current task).
  3. If Activityhas 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 Activityis created at the root of the task and this task is put on top of the current task (so that when Activityfinishes, the user is dropped back into the current task).
  1. 如果Activity与当前任务具有相同的任务关联(即:执行此代码的任务),它将清除当前任务(完成任务中的所有活动)并Activity在当前任务中启动一个新实例。如果用户按下 BACK 键,这将完成Activity并完成当前任务(因为任务中只有 1 个活动)并将用户返回到主页屏幕或启动此任务的任务(在下面的任务)任务堆栈中的此任务)。
  2. 如果Activity具有与当前任务不同的任务亲缘关系,并且已经存在具有该任务亲缘关系的现有任务(即:Activity属于的现有任务),则该现有任务被置于前台,清除(所有活动在任务完成),Activity在任务的根创建一个新的实例,这个任务被放在当前任务的顶部(这样当Activity完成时,用户会被放回到当前任务中)。
  3. 如果Activity具有与当前任务不同的任务亲缘关系,并且没有具有该任务亲缘关系的现有任务,则创建一个新Activity任务并在任务的根创建一个新实例,并将此任务放在当前任务的顶部任务(这样当Activity完成时,用户会被放回到当前任务中)。


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);

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

...将做以下事情之一取决于...

  1. If Activityhas the same task affinity as the current task (ie: the task from which this code is executing) and Activityis 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).
  2. If Activityhas the same task affinity as the current task (ie: the task from which this code is executing) and Activityis notthe root activity of the current task, this will simply create a new instance of Activityand 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).
  3. If Activityhas a different task affinity than the current task, and there is already an existing task with that task affinity (ie: an existing task that Activitywould belong to) and Activityis 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 notto the task that started that task).
  4. If Activityhas a different task affinity than the current task, and there is already an existing task with that task affinity (ie: an existing task that Activitywould belong to) and Activityis notthe 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 notto the task that started that task) and a new instance of Activityis created and put on top of any existing activities in that task.
  5. If Activityhas 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 Activityis created at the root of the task and the new task is decoupled from the task stack (so that when Activityfinishes, the user is returned to the HOME screen and notto the task that started it).
  1. 如果Activity与当前任务具有相同的任务关联(即:执行此代码的任务)并且Activity是当前任务的根活动,则不会执行任何操作。它不会启动新任务,不会清除任何活动,不会创建 的新实例Activity,也不会更改当前任务完成时发生的行为(即:如果当前任务是由另一个任务,当当前任务中的所有活动都完成时,它会将用户放回到任务堆栈中的前一个任务中)。
  2. 如果Activity有相同的任务亲和力当前的任务(即:从该代码执行的任务),并Activity不是当前任务的根系活力,这将只需创建一个新的实例Activity,并把它放在当前活动的顶部在当前任务中。它不会启动新任务,不会清除任何活动,也不会改变当前任务完成时发生的行为(即:如果当前任务是由另一个任务启动的,当当前任务中的所有活动任务完成后,它会将用户放回到任务堆栈中的前一个任务中)。
  3. 如果Activity具有与当前任务不同的任务亲缘关系,并且已经存在具有该任务亲缘关系的现有任务(即:Activity将属于的现有任务)并且Activity是该现有任务的根活动,则该现有任务被带到前台和该任务与任务堆栈分离(即:当该任务中的所有活动都完成时,它会将用户返回到主页屏幕而不是启动该任务的任务)。
  4. 如果Activity有比当前任务不同的任务亲和力,且已经存在与该任务亲和力现有任务(即:一个现有的任务Activity将属于),并Activity没有的,现有的任务,那么,现有的任务带来的根系活力到前台并且该任务与任务堆栈分离(即:当该任务中的所有活动都完成时,它会将用户返回到主页屏幕而不是启动该任务的任务)并Activity创建一个新实例并置于该任务中的任何现有活动之上。
  5. 如果Activity与当前任务具有不同的任务亲缘关系,并且没有具有该任务亲缘关系的现有任务,则创建一个新Activity任务并在任务的根处创建一个新实例,并将新任务从任务堆栈中解耦(这样当Activity完成时,用户将返回到主页屏幕而不是启动它的任务)。


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);

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

...将做以下事情之一,具体取决于...

  1. If Activityhas 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 Activityinto the current task. If the user presses the BACK key, this will finish Activityand also finish the current task (since there is only 1 activity in the task) and return the user to the HOME screen.
  2. If Activityhas a different task affinity than the current task, and there is already an existing task with that task affinity (ie: an existing task that Activitywould belong to), then that existing task is brought to the foreground, cleared (all activities in the task are finished), a new instance of Activityis created at the root of the task and this task is decoupled from the task stack (so that when Activityfinishes, the user is returned to the HOME screen).
  3. If Activityhas 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 Activityis created at the root of the task and this task is decoupled from the task stack (so that when Activityfinishes, the user is returned to the HOME screen).
  1. 如果Activity与当前任务具有相同的任务关联(即:执行此代码的任务),它将清除当前任务(完成任务中的所有活动)并Activity在当前任务中启动一个新实例。如果用户按下 BACK 键,这将完成Activity并完成当前任务(因为任务中只有 1 个活动)并将用户返回到主页屏幕。
  2. 如果Activity具有与当前任务不同的任务亲缘关系,并且已经存在具有该任务亲缘关系的现有任务(即:Activity属于的现有任务),则该现有任务被置于前台,清除(所有活动在任务完成),Activity在任务的根创建一个新的实例,这个任务从任务堆栈中解耦(这样当Activity完成时,用户返回到主屏幕)。
  3. 如果Activity与当前任务具有不同的任务亲缘关系,并且没有具有该任务亲缘关系的现有任务,则会创建一个新Activity任务并在该任务的根创建一个新实例,并将该任务从任务堆栈中解耦(以便当Activity完成时,用户返回到主页屏幕)。


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 Activityhas a special launch mode)...

我意识到这个答案很长很复杂,但有很多不同的情况。我可能还没有涵盖所有可能的情况(例如,如果Activity有特殊的启动模式)...