The Activity of the LaunchMode

By default, when we start the same Activity more than once, we create multiple instances and place them in the task stack one by one. When we press the Back key, these activities are dropped again and again. The task stack is a “last in, first out” stack structure, that is, every time you press the back button, an Activity is removed from the stack until it is empty. When there are no activities in the stack, the system will reclaim the task stack. There are four startup modes: Standard, singleTop, singleTask and singleInstance. The following describes the meanings and usage examples of various startup modes.

Standard – Default mode

Standard mode is the default mode of the system. Each time an Activity is started, a new instance is created, regardless of whether the instance already exists. In this mode, whoever started the Activity runs on the same stack as the Activity that started it. If Activity A starts Activity B (which is standard mode), then B will be added to A’s stack.

SingleTop — Top of stack reuse mode

SingleTop stack top reuse mode. If the new Activity is already at the top of the task stack, the Activity is not recreated and its onNewIntent method is called, with arguments to retrieve information about the current request. Note that the Activity’s onCreate and onStart methods will not be re-invoked by the system because they have not changed. If an instance of the new Activity already exists but is not at the top of the stack, the new Activity will still be rebuilt. The content display page is suitable for receiving notifications. When multiple news feeds are received, the Activity used to display news is set to this mode. Different news information is displayed based on incoming Intent data, and multiple activities are not started.

SingleTask – In-stack reuse mode

SingleTask: In-stack reuse mode. This is a single-instance mode, in which as long as the Activity is in a stack, the instance is not recreated by launching the Activity multiple times, and all activities above it are removed from the stack when reused, and its onNewIntent method is called. There is a task stack match in this process because the pattern starts looking for instances in the desired task stack, which is specified by the taskAffinity property, and creates the task stack if it does not exist. TaskAffinity identifies the name of the task stack required by an Activity. By default, the name of the task stack required by all activities is the package name of the application. You can specify the TaskAffinity attribute individually for each Activity, and this attribute must not be the same as the package name, otherwise it is not specified. The TaskAffinity attribute is used primarily in pairs with the singleTask start mode or allowTaskReparenting attribute. In addition, the task stack is divided into the foreground task stack and the background task stack. The Activity in the background task stack is suspended, and the user can switch the background task stack to the foreground again. It is suitable for the program entry point, such as the main interface of the browser. No matter how many applications you start the browser from, the main interface will only be started once. In other cases, the browser will go to onNewIntent, and other pages on the main interface will be cleared

4. SingleInstance — singleInstance mode

SingleInstance: single-instance mode. In addition to all the features of singleTask mode, the Activity in this mode can only be located in a singleTask stack, which has global uniqueness, that is, there is only one instance in the whole system. Due to the feature of in-stack reuse, subsequent requests will not create new Activity instances. Unless this particular task stack is destroyed. An Activity started in singleInstance mode is a singleton in the whole system. If there is an instance of such Activiyt when it is started, its task will be scheduled to the foreground and the instance will be reused

Forward: Four Android startup modes