To set the activity startup mode in Androidmanifest.xml:
<activty android:name=".MainActivity" android:launchMode="standard" />
1.standard
Each time the activity is activated, the activity is created and placed in the task stack. This is the default startup mode for the system.
Standard mode creates a new activity object each time, and when you click the Back button, he destroys the top of the stack (the current activity) and jumps to the next level, but when you click the button to create the object again in this activity, It will create new activity objects in addition, which may not be what we need in most cases because of the excessive consumption of system performance.
2.singleTop
If there is an instance of the activity at the top of the task, reusing the instance will create a new instance and put it in the top of the stack (even if the activity instance already exists on the stack, as long as it is not on top of the stack, an instance will be created).
The current activity at the top of the stack is automatically detected each time a new activity is used, whether it is an activity that needs to be referenced, or if it is directly referenced, without creating a new activity, when it does not need to create a new activity object. It calls the Onnewintent () method.
Avoid a bad user experience, if the interface is already open and at the top of the stack of tasks, it will not be opened repeatedly
Application Scenarios:
There are many scenarios for singletop, which are generally applicable to activity that can be reused and have multiple open channels, to avoid re-opening once an activity has been opened and the focus has been gained. For example, the bookmarks page of the Android system browser is an activity in singletop mode. Android's browser is written based on the WebKit kernel, which supports JavaScript scripting languages, and can be used to set browser bookmarks through JavaScript scripts, so that if there are multiple pages with a bookmark-saving JavaScript script, will cause the bookmark page to be opened multiple times, so the bookmark page is set to Singletop mode, which avoids repeatedly opening the bookmark page when you save multiple bookmarks.
3.singleTask
If an instance of the activity is already in the stack, the instance is reused (Onnewintent () of the instance is invoked). When reused, the instance is returned to the top of the stack, so the instances above it will be removed from the stack. If the instance does not exist in the stack, a new instance is created to be put into the stack.
This startup mode and Singletop in the name can see the difference, that is, singletop only detect the current stack at the top of the activity is we need to request to create, and Singletask will detect the stack of all activity objects, from the top down, If it detects what we are requesting, it will destroy the object above the activity object and put the detected activity to the top of the stack directly.
The activity of the "Singletask" startup mode is set, and when it starts, it finds the property value in the system affinity equals its property value taskaffinity the task exists; If there is such a task, it will start in this task, Otherwise, it will start in the new task. Therefore, if we want to set the activity of the "Singletask" startup mode to start in a new task, set a separate Taskaffinity property value for it.
Application Scenarios:
Browser: The underlying use of the WebKit C kernel, initialization needs to request a lot of memory resources, CPU time, so use singletask, to ensure that only one instance in the task stack exists
Singletask The application scenario for the general application of the main page, when the fallback to the main page, clear the Backstack, all the activity on it, so as to avoid the confusion of the program navigation logic. such as the main page of the browser of the Android system, is Singletask mode, mentioned above, Android under the browser is the WebKit kernel, it is written in C language, and each time you open a new Web page if you reopen an activity, is very resource-intensive (parsing HTML, script scripts), so it is set to Singletask mode, so that in the browser application, no matter how many pages are opened, the same activity is used. So if there is a resource-intensive activity in the future, consider using the Singletask open mode
4.singleInstance (equivalent to instance)
Create the activity instance in a new stack and have multiple apps share that activity instance in the stack. Once an instance of the activity of the pattern exists on a stack, any application that activates the activity will reuse the instance in that stack, which would have the effect of sharing an app with multiple applications, regardless of who activates the activity into the same application.
This startup mode works similarly to the browsers we use, and we all know that when accessing a browser in multiple programs, if the current browser is not open, the browser opens, otherwise it will be accessed in the currently open browser. This mode saves a lot of system resources because he can guarantee that the activity object to be requested only exists in the current stack.
The call page of the Android system, multiple calls are used by an activity.
Note: Android startup mode is not related to process, process is process, task stack is management activity
Four startup modes for Android activity