Android advanced advanced in-depth analysis of the four startup modes

Android advanced advanced in-depth analysis of the four startup modes

In-depth analysis of the four startup modes

Activity's startup mode is also a difficult point, because the various startup modes and flags are too easy to confuse, but Activity is the first of the four components, it is indeed very important, in order to meet the needs of the project, you must use the Activity startup mode.

1. Activity 的 LaunchMode

1.1 Startup mode

The reason why Activity uses the startup mode is that the creation of Activity is in the task stack. When we start the same Activity, the system will create multiple Activity instances and put them in the task stack. When we press the back key, the task stack Instances will pop out of the stack one by one. I think the stack is not unfamiliar, with the characteristics: first in, first out. If we do not allow the system to create the same Activity repeatedly, we will use the startup mode of the Activity to set it. Activity's startup mode is divided into four standard, singleTop, singleTask and singleInstance, the previous article also has related introductions, and I will briefly mention them below.

(1) Standard standard mode:

This is the default startup mode of the system. Every time an Activity is started, a new instance will be created, regardless of whether the instance exists. If A starts B, B's activities will enter A's task stack.

(2) SingleTop stack top multiplexing mode:

In this startup mode, the new Activity is already at the top of the stack. If you start the Activity again, the Activity will not be recreated. At the same time, the system's onNewIntent method is called back. Through the parameters of this method, we can retrieve the current request information. Of course, the onCreate, onStart and onResume methods of Activity will not be called again. If the Activity is not at the top of the stack, the activity will be recreated.

(3) SingleTask stack multiplexing mode:

This is a singleton mode. In this mode, as long as the instance exists in the stack, the instance will not be recreated. For example, if we want to create an instance A, the system will first determine whether the same instance as A exists in the task stack. If the instance exists in the task stack, the system will call A to the top of the stack and call its onNewIntent method. At the same time, Activity instances above A will be removed from the stack until A is at the top of the stack; if the instance does not exist, the system will Innovation creates a new instance A and pushes it to the top of the stack.

(3) SingleInstance single instance mode:

I usually call this type of Activity an enhanced version of the singleTask mode. In addition to having the characteristics of the singleTask mode, the Activity instance started with singleInstance exists in a separate task stack, and subsequent requests will not create a new instance.

1.2 Task stack

What is a task stack? How are each Activity allocated to each task stack? The following situations are all in the singleTask mode.

definition:

Speaking of a parameter, TaskAffinity translates to the relevance of the task. This parameter identifies the name of the task stack required by an Activity. By default, the name of the task stack required by the Activity is the application package name. The task stack is divided into a foreground task stack and a background task stack. The Activity in the background task stack is in a suspended state, and the user can switch the background task stack to the foreground. -Task stack

1.3 Two ways to set the startup mode

The first way: Set the startup mode through the AndroidMenifest configuration file.


 1<activity
 2      android:name=".MonitoringActivitys.MonitorActivity"
 3       android:label="@string/title_activity_monitor"
 4       android:theme="@style/AppTheme.NoActionBar"
 5       android:launchMode="singleTask">
 6       <intent-filter>
 7           <action android:name="android.intent.action.MAIN" />
 8           <category android:name="android.intent.category.LAUNCHER" />
 9       </intent-filter>
10 </activity>

The second way: Set the startup mode by setting the flag in the Intent.

1Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
2intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
3startActivity(intent);

the difference:

① The second priority is higher than the first

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

Guess you like

Origin blog.51cto.com/15064450/2602811