This knowledge is estimated to be written rotten, but in order to impress myself, I still want to write a. >. <

Note: Both CLS are class objects for the Activity

1.standard

By default, A new instance of an Activity is created each time it is started. A task stack allows multiple instances of the Activity to exist. The started Activity is automatically added to the task stack that starts it. B is automatically added to task stack 1.

AndroidManifest.xml

<activity
android:launchMode="standard"/>
Copy the code

Java code

Context is the Activity instance

Intent intent = new Intent(context,  cls);
context.startActivity(intent);
Copy the code

Context is not an Activity instance Note: The taskAffinity for this Activity must be the default or not set

Intent intent = new Intent(context,  cls);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
Copy the code

2.singleTop

In this startup mode, if no instance of the Activity is on the task stack, an instance is created and placed at the top of the stack. If an instance is on the task stack but not on the top, create another instance and put it on the top. If there is an instance on the task stack and it is at the top of the stack, no more instances are created.

If the sequence of activities from bottom to top is [A, B, C], and C is singleTop, no instance of C will be created. If the order from bottom to top of stack is [A, C, B], start C, then create A new instance of C and put it at the top of stack [A, C, B, C].

AndroidManifest.xml

<activity
android:launchMode="singleTop"/>
Copy the code

Java code

Context is the Activity instance

Intent intent = new Intent(context,  cls);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
context.startActivity(intent);
Copy the code

Context is not an Activity instance

Intent intent = new Intent(context,  cls);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
context.startActivity(intent);
Copy the code

3.singleTask

This is an in-stack singleton mode for an Activity, meaning that as long as an instance of the Activity exists in the task stack, it is not created again when it is started. When an Activity is started, if no instance of the Activity exists in its task stack, an instance is created and pushed to the top of its task stack. If its task stack already has an instance of the Activity, it clears all activities in the task stack that precede the Activity.

Task stack 1 has A sequence of **[A, B, C] from the bottom of the stack to the top of the stack. If D is started in singleTask mode, then D instance is created and placed in task stack 1[A, B, C, D]. The sequence of task 1 from bottom to top is [A, B, C]. When B is singleTask mode, no instance of A is created, all activities on A are cleared, and the task stack changes to [A, B]**.

AndroidManifest.xml

<activity
android:launchMode="singleTask"/>
Copy the code

Java code

Context is the Activity instance

Intent intent = new Intent(context,  cls);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(intent);
Copy the code

Context is not an Activity instance

Intent intent = new Intent(context,  cls);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(intent);
Copy the code

4.singleInstance

This is a singleton mode of Activity that has all the features of singleTask, and the system creates a separate task stack for an Activity that only allows the Activity to exist.

AndroidManifest.xml

<activity
android:launchMode="singleInstance"/>
Copy the code

Java code

Context is the Activity instance

Did not findCopy the code

Context is not an Activity instance

Did not findCopy the code

Other: onNewIntent (Intent Intent)

When an Activity is launched using a singleTop, singleTask, or singleInstance instance, if the instance does not need to be created again, the onNewIntent(Intent Intent) of the existing instance is called. A new Intent is passed into onNewIntent(Intent Intent).