A task is a collection of activities that users interact with when performing a certain job. The activities are arranged in a stack (the "back stack"), in the order in which each activity is opened.
"standard"
(the default mode)
"singleTop"
onNewIntent()
method, rather than creating a new instance of the activity. The activity can be instantiated multiple times, each instance can belong to different tasks, and one task can have multiple instances (but only if the the activity at the top of the back stack is
not
an existing instance of the activity).
For example, suppose a task's back stack consists of root activity A with activities B, C, and D on top (the stack is A-B-C-D; D is on top). An intent arrives for an activity of type D. If D has the default "standard"
launch mode, a new instance of the class is launched and the stack becomes A-B-C-D-D. However, if D's launch mode is "singleTop"
, the existing instance of D receives the intent through onNewIntent()
, because it's at the top of the stack—the stack remains A-B-C-D. However, if an intent arrives for an activity of type B, then a new instance of B is added to the stack, even if its launch mode is "singleTop"
.
Note: When a new instance of an activity is created, the user can press the Back button to return to the previous activity. But when an existing instance of an activity handles a new intent, the user cannot press the Back button to return to the state of the activity before the new intent arrived inonNewIntent()
.
"singleTask"
onNewIntent()
method, rather than creating a new instance. Only one instance of the activity can exist at a time.
Note: Although the activity starts in a new task, the Back button still returns the user to the previous activity.
"singleInstance"
.
"singleTask"
, except that the system doesn't launch any other activities into the task holding the instance. The activity is always the single and only member of its task; any activities started by this one open in a separate task.
Using Intent flags
When starting an activity, you can modify the default association of an activity to its task by including flags in the intent that you deliver to startActivity()
. The flags you can use to modify the default behavior are:
FLAG_ACTIVITY_NEW_TASK
onNewIntent()
.
This produces the same behavior as the "singleTask"
launchMode
value, discussed in the previous section.
FLAG_ACTIVITY_SINGLE_TOP
onNewIntent()
, instead of creating a new instance of the activity.
This produces the same behavior as the "singleTop"
launchMode
value, discussed in the previous section.
FLAG_ACTIVITY_CLEAR_TOP
onNewIntent()
).
There is no value for the launchMode
attribute that produces this behavior.
FLAG_ACTIVITY_CLEAR_TOP
is most often used in conjunction with FLAG_ACTIVITY_NEW_TASK
. When used together, these flags are a way of locating an existing activity in another task and putting it in a position where it can respond to the intent.
Note: If the launch mode of the designated activity is "standard"
, it too is removed from the stack and a new instance is launched in its place to handle the incoming intent. That's because a new instance is always created for a new intent when the launch mode is "standard"
.