Android four major component Activity startup modes

1.standard standard mode

Every time an Activity is started (hereinafter represented by Ac), an instance will be created, regardless of whether the instance exists.
Typical multi-instance implementation, a task stack can contain multiple instances, and each instance can also belong to a different task stack.
Who starts Ac, the new Ac runs in the stack of the Ac that started it

2.singleTop stack top reuse

If the new Ac is already at the top of the task stack, it will not be created after restarting, and the onNewIntent() method callback will be triggered at the same time . The parameters of this method can take out the currently requested information.

If it is not on the top of the stack, create a new one and call onCreate(), onStart()

For example: ABCD, four Ac in the stack, A is at the bottom of the stack, and D is at the top of the stack. Start D again, and the startup mode is singleTop, the stack will still be ABCD.

If the startup mode is standard, D will be rebuilt and the stack will be ABCDD.

3.singleTask in-stack reuse

As long as Ac exists in the stack, it will not be re-created upon restart. Like singleTop, the onNewIntent() method will be called back
. If the required task stack is S1, and the task stack S1 is ADBC, the stack reuse principle, D does not Rebuild, cut to the top of the stack, and call onNewIntent(), singleTask has clearTop effect, all Ac on D are popped off the stack, and finally AD

4. singleInstance single instance mode

The enhanced singleTask has all the features of the singleTask mode. Another enhancement is that the Ac in this mode can only be located in one task stack alone. Any other Ac started by this Ac will be placed in other task stacks.

Specify startup mode:

1. Specify for Activity through AndroidManifest

For example:

<activity

android:name=“com.xxx.MyActivity”

android:launchMode=“singleTask”/>

2. Specify the activity by setting the flag in the intent

For example:

Intent intent = new Intent();

intent.setClass(MainActivity.this,MyActivity.class);

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

startActivity(intent)
Note: Method 2 has higher priority

The other two methods differ in their limited scope, such as

The first method cannot directly set the FLAG_ACTIVITY_CLEAR_TOP flag for the Activity, and the second method cannot specify the singleInstance mode for the Activity.

Commonly used Flags for Activity:

1.FLAG_ACTIVITY_NEW_TASK:

Specify singleTask startup mode, its effect is the same as specified in xml

accomplish:

Find whether there is a task stack with taskAffinitity related to the startup Ac task. The
Ac task correlation of the same APP is the same, and the default is the package name.
Yes, move the entire stack to the foreground, and the old one in the stack remains unchanged. Start Ac and push it into the stack
. , then create a new stack for storage
. Note: If Ac is started with ApplicationContext, FLAG_ACTIVITY_NEW_TASK must be added, otherwise an exception will be reported.

2.FLAG_ACTIVITY_SINGLE_TOP:

Specify singleTop startup mode, which is the same as xml specification

3.FLAG_ACTIVITY_CLEAR_TOP:

When an Ac with this mark is started, the Ac above it in the same stack will be popped out of the stack.

This mode generally needs to be used in conjunction with FLAG_ACTIVITY_NEW_TASK. At this time, if the activated Ac exists, its onNewIntent() will be called.

If the started Ac is started in standard mode, it and the Ac above will be popped off the stack, and the system will create a new Ac instance and push it onto the stack.

4.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS:

Ac with this mark will not appear in the historical Ac list, which can be used when you do not want the user to return to the current Ac through the history list

The attribute Android of Ac specified in xml: excludeFromRecents="true"

5.FLAG_ACTIVITY_REORDER_TO_FRONT

ABCD in the stack, start B, become ACDB

task stack

The parameter TaskAffinity (task affinity) identifies the name of the task stack required by an Ac.

By default, the names of all task stacks required by Ac are the application package names.

The TaskAffinity attribute is mainly used in conjunction with the singleTask startup mode or allowTaskReparenting attribute , and has no meaning in other cases.

In addition: the task stack is divided into a foreground task stack and a background task stack. Ac in the background task stack is in a paused state. The user can bring the background task stack to the foreground again

Scenes:

TaskAffinity is used in conjunction with the singleTask startup mode. It is the name of the current task stack of the Ac with this mode. The Ac to be started will run in the task stack with the same name as TaskAffinity. When the TaskAffinity and allowTaskReparenting attributes are combined, it is special. For example, there
are
two Applying A and B, A starts an Ac C of B, then presses the HOME key to return to the desktop, and then clicks the B app. At this time, it is not B's main app that is started, but Ac C. Because at this time C is transferred from A's task stack to B's task stack.

Principle: When B starts, B creates its own task stack. At this time, the system finds that C has been created, so it transfers C from A's task stack.

Guess you like

Origin blog.csdn.net/qq_32670947/article/details/132490504