I am in the process of (finally) writing the chapter on tasks for my book, and I am encountering a few lingering puzzles.
Things that serve as home screen launchers seem to use the combination of FLAG_ACTIVITY_NEW_TASK
and FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
when they launch the requested launcher activity:
Intent i=new Intent(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_LAUNCHER);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
i.setComponent(name);
startActivity(i);
The documentation for FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
has:
If set, and this activity is either being started in a new task or bringing to the top an existing task, then it will be launched as the front door of the task. This will result in the application of any affinities needed to have that task in the proper state (either moving activities to or from it), or simply resetting that task to its initial state if needed.
That's not especially clear.
In particular, it would seem that the same effects would be seen using a combination of FLAG_ACTIVITY_CLEAR_TOP
and FLAG_ACTIVITY_SINGLE_TOP
. Quoting the docs for FLAG_ACTIVITY_CLEAR_TOP
:
If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent...
The currently running instance of [the desired activity] will either receive the new intent you are starting here in its onNewIntent() method, or be itself finished and restarted with the new intent. If it has declared its launch mode to be "multiple" (the default) and you have not set FLAG_ACTIVITY_SINGLE_TOP in the same intent, then it will be finished and re-created; for all other launch modes or if FLAG_ACTIVITY_SINGLE_TOP is set then this Intent will be delivered to the current instance's onNewIntent().
The FLAG_ACTIVITY_CLEAR_TOP
documentation makes sense, at least to me.
So, what does FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
do that is different than the combination of FLAG_ACTIVITY_CLEAR_TOP
and FLAG_ACTIVITY_SINGLE_TOP
?
Bonus points if you can explain what FLAG_ACTIVITY_CLEAR_TASK
does that is different from either of the other two options described above.
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.
One obvious difference between this and FLAG_ACTIVITY_CLEAR_TOP
| FLAG_ACTIVITY_SINGLE_TOP
is that FLAG_ACTIVITY_CLEAR_TASK
needs FLAG_ACTIVITY_NEW_TASK
. But, other than that, it would seem like the net effects are the same, and also match FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
.