Activity's startup mode and onNewIntent

1. Introduction to launch mode

Launch mode is simply the strategy when Activity is launched. It is set in the android:launchMode attribute of the tag in Android Manifest.xml;

there are 4 launch modes, namely standard, singleTop, singleTask, and singleInstance.

The task stack of Activity, the task stack is a kind of "first in, last out" stack structure, which is easy to understand,

that until the task stack is empty, when the task stack is empty. When it is empty, the system recycles the task stack.

Second, the understanding of the four major startup modes

1.standard: Standard mode, which is also the default startup mode of the system. Every time an Activity is started, a new instance will be recreated, regardless of whether the instance already exists. Who starts this Activity, then this Activity runs in the task stack of the Activity point that started him, but when we use Application to start an Activity in standard mode, an error will be reported, because there is no so-called task stack for non-Activity type Content. At this time, we need to specify a flag bit FLAG_ACTIVITY_NEW_TASK for the Activity to be started, so that when starting, a task stack is created for this Activity, which is actually the way to start SingleTask.

2.singleTop: stack top multiplexing mode. In this mode, if the new Activity is already at the top of the task stack, and the Activity is started again at this time, the Activity will not be created, that is, the Oncreate and onStart methods of the Activity will not be called, and its OnNewIntent will not be called. The method will be called, and the information of the current request can be retrieved through the parameters of this method. If this Activity is not at the top of the stack, an instance of this Activityty will be created.

3.singleTask: In-stack multiplexing mode. It is a single instance mode. In this case, as long as an Activity exists in a stack, the instance will not be recreated when the Activity is started multiple times. Like SingleTop, it will also execute the OnNewIntent method, that is, when an Activity After the Activity of singleTask is started, the system will find out whether there is a task stack that the Activity wants. If so, see if the Activity has been instantiated in the task stack. If not, it will re-create the task stack of an Activity.

4.singleInstance: single instance mode. That is to say, it is an enhanced singleTask. In addition to all the tasks of the singleTask, the Activity of this startup mode can only be located in a single task stack.

The task stack consists of the foreground task stack and the background task stack. When there is an activity in the background task stack that needs to be started, the entire background task stack will be in the current task stack. The task stack required by singleTask. By default, the task stack required by Activity is the package name of the application. Of course, we can also specify the task stack for each Activity separately through the attribute of TaskAffinity. The task stack specified by TaskAffinity cannot be this The package name of the application. The startup mode of the Activity can be specified by setting the flag bit for the Intent and in the registration file. The priority of the two flag bits is high.

3. Activity's Falgs flag

There are many Flags in Activity. We mainly talk about the commonly used ones,
FLAG_ACTIVITY_NEW_TASK. The function of this flag is to specify the "singleTask" startup mode for Activity, which is the same as specifying the effect in
XMl; FLAG_ACTIVITY_SINGLE_TOP, the role of this flag is Specify the "singleTop" startup mode for the Activity;
FLAG_ACTIVITY_CLEAR_TOP, used in conjunction with FLAG_ACTIVITY_NEW_TASK, means that all activities above the activity in the same task stack must be popped;
FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS, activities with this flag will not appear in the user's history list for this activity.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326356793&siteId=291194637