Four startup modes of android activity
Set the Activity Startup Mode in AndroidManifest. xml:
1. standard
Every time an activity is activated, an activity is created and placed in the task stack. This is the default startup mode.
In Standard mode, a new Activity object is created every time. When the return button is clicked, it eliminates the top stack (current Activity) and jumps to the next layer, however, when you click the button again in this Activity to create an object, it will create another Activity object. This mode may not be required in most cases, because the consumption of system performance is too high.
2. singleTop
If an instance of the Activity exists at the top of the stack of the task, the instance is reused. Otherwise, a new instance is created and placed on the top of the stack (even if the Activity instance already exists in the stack, instances will be created as long as they are not at the top of the stack ).
Each time a new Activity is used, the system automatically checks whether the current Activity at the top of the stack is the Activity to be referenced. If yes, the Activity is directly referenced without creating a new Activity; when it does not need to create a new Activity object, it will call the onNewIntent () method.
Avoid a bad user experience. If this interface has been opened and is on the top of the stack of the task stack, it will not be enabled again.
Application scenarios:
SingleTop has many application scenarios. It is generally applicable to multiple open channels that can be reused. This avoids repeated enabling of an Activity when it is enabled and focused. For example, the bookmarks page of the Android browser is a singleTop Activity. Android browsers are written based on the WebKit kernel. They support the JavaScript scripting language. You can use JavaScript scripts to set browser bookmarks. In this way, if multiple pages have JavaScript scripts for saving bookmarks, this will cause the bookmarked page to be opened multiple times. Therefore, the bookmarked page is set to singleTop mode, which prevents repeated bookmarked pages when multiple bookmarks are saved.
3. singleTask
If an instance of the Activity already exists in the stack, the instance will be reused (onNewIntent () of the instance will be called ()). When it is reused, the instance will be sent back to the top of the stack, so the instance on it will be removed from the stack. If this instance does not exist in the stack, a new instance is created and put into the stack.
The difference between this startup mode and singleTop is that singleTop only checks whether the Activity at the top of the current stack needs to be created, singleTask detects all Activity objects in the stack. From top to bottom, if we detect that the Activity object is requested, the objects above the Activity object will be eliminated, directly set the detected Activity to the top of the stack.
When an Activity in the singleTask startup mode is set, it first finds the task with the attribute value affinity equal to its attribute value taskAffinity in the system. If such a task exists, it will be started in this task, otherwise it will be started in the new task. Therefore, if we want to set the "singleTask" Startup Mode for an Activity to start in a new task, we need to set an independent taskAffinity attribute value for it.
Application scenarios:
Browser: The underlying layer uses the webkit c kernel. during initialization, many memory resources need to be applied for, occupying cpu time. Therefore, singletask is used to ensure that only one instance exists in the task stack.
SingleTask is applicable to the home page of a general program. When it is rolled back to the home page, all the activities on the BackStack are cleared to avoid confusion in the program navigation logic. For example, the main page of the Android browser is the singleTask mode. As mentioned above, the android browser is based on the Webkit kernel, which is written in C language, every time you open a new webpage, if you re-open an Activity, it will consume a lot of system resources (HTML and Script scripts need to be parsed). Therefore, it is set to singleTask mode. In this way, in browser applications, no matter how many pages are opened, the same Activity is used. Therefore, if there is an Activity that consumes system resources in the future, you can consider using singleTask to enable the mode.
4. singleInstance (equivalent to an instance)
Create the Activity instance in a new stack and allow multiple applications to share the Activity instance in the stack. Once an instance of the Activity in this mode exists in a stack, any application that activates the Activity will reuse the instance in the stack. The effect is equivalent to sharing one application by multiple applications, no matter who activates the Activity, it will enter the same application.
This startup mode works similar to the browser we use. We all know that when accessing a browser in multiple programs, if the current browser does not open, open the browser, otherwise, it will be accessed in the current browser. This mode saves a lot of system resources because it ensures that only one Activity object to be requested exists in the current stack.
The call page of the Android system uses an Activity for multiple calls.
Note: The Android startup mode has nothing to do with the process. The process is a process and the task stack is used to manage the Activity.