There is a difference between the 2 snippets. Here’s some important background information:
-
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 taskB, taskBis stacked on top of taskA. If the user presses the BACK key enough times in taskB, he will eventually end up back in task `A. This is standard Android behaviour.
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…
- 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 ofActivityinto the current task. If the user presses the BACK key, this will finishActivityand 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
Activityhas a different task affinity than the current task, and there is already an existing task with that task affinity (ie: an existing task thatActivitywould belong to), then that existing task is brought to the foreground, cleared (all activities in the task are finished), a new instance ofActivityis created at the root of the task and this task is put on top of the current task (so that whenActivityfinishes, the user is dropped back into the current task). - 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 ofActivityis created at the root of the task and this task is put on top of the current task (so that whenActivityfinishes, the user is dropped back into the current task).
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…
- If
Activityhas the same task affinity as the current task (ie: the task from which this code is executing) andActivityis 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 ofActivity, 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
Activityhas the same task affinity as the current task (ie: the task from which this code is executing) andActivityis not the root activity of the current task, this will simply create a new instance ofActivityand 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
Activityhas a different task affinity than the current task, and there is already an existing task with that task affinity (ie: an existing task thatActivitywould belong to) andActivityis 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
Activityhas a different task affinity than the current task, and there is already an existing task with that task affinity (ie: an existing task thatActivitywould belong to) andActivityis 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 ofActivityis created and put on top of any existing activities in that task. - 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 ofActivityis created at the root of the task and the new task is decoupled from the task stack (so that whenActivityfinishes, 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);
…will do 1 of the following things, depending…
- 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 ofActivityinto the current task. If the user presses the BACK key, this will finishActivityand also finish the current task (since there is only 1 activity in the task) and return the user to the HOME screen. - 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 thatActivitywould belong to), then that existing task is brought to the foreground, cleared (all activities in the task are finished), a new instance ofActivityis created at the root of the task and this task is decoupled from the task stack (so that whenActivityfinishes, the user is returned to the HOME screen). - 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 ofActivityis created at the root of the task and this task is decoupled from the task stack (so that whenActivityfinishes, 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)…