Android Startup Mode

Source: Internet
Author: User

In an Android Application, multiple activities are inevitably included. When multiple activities are enabled, when you press the back key, the previous Activity is generally rolled back, this is a bit similar to the browser, but it should be noted that the Android system can only roll back to the previous Activity and cannot move forward to the next Activity, to maintain this function, you need to understand the concept of a Back Stack.
What is rollback stack?
First, let's take a look at the relationship between applications and processes. As we all know, there are four main components in Android: Activity, Service, explorer, and ContentProvider. Generally, developing an application contains multiple Android components. Therefore, an application is a collection of components, and a process is the carrier for running these components.
The Back Stack is only for the Activity. It is used to maintain the user interface experience, making a Task feel like an application, regardless of whether the Activity comes from the same application, do not confuse the rollback stack with the process. The Home page of a device is the starting position of most tasks. When you click an application icon, the application Task will arrive at the front-end, the main Activity of the application is pushed to the top of the BackStack stack and the focus is obtained. This Activity is called the root Activity. The Activity in the BackStack can pop up and destroy the stack by clicking the rollback key, in this case, the focus of the previous Activity will be obtained until the user returns to the Home Page. When all the activities in the BackStack are popped up and destroyed, the Task no longer exists, however, the process of this program still exists (not destroyed at this time ).
Task status
As described above, each Task has a BackStack, and multiple tasks can exist in the system, but only one Task gets the foreground focus at a time. Generally, the system allows you to switch among multiple tasks, and the Activity in the background Task will be put in the Stopped state. In fact, as long as the Activity in the same Task does not exist at the top of the stack and gets the foreground focus, it is a Stopped state. Lower
Activity Startup Mode
Depending on the Startup Mode of the Activity, its status in BackStack is different. Activity can be configured through the AndroidManifest. xml configuration file, and set the android: launchMode attribute in the node. It has four options:
· Standard
· SingleTop
· SingleTask
· SingleInstance
Standard:
The standard start mode is also the default start mode. If the android: launchMode attribute is not set. In standard mode, the Activity is pushed to the BackStack according to the starting order.
In the standard mode, the stack pressure and rollback operations of the Activity are as follows:

SingleTop:
Single-top mode. In this Activity startup mode, if the Activity already exists at the top of the BackStack stack when an Activity is started, the Activity will not be re-created, instead, you can reuse the existing Activity on the top of the stack to avoid opening the same Activity repeatedly.

Is it singleTZ? Http://www.bkjia.com/kf/ware/vc/ "target =" _ blank "class =" keylink "> cipher + CjxwPjxpbWcgc3JjPQ =" http://www.2cto.com/uploadfile/Collfiles/20140310/20140310140210338.jpg "alt =" \ ">

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.
SingleTask:
When you start an Activity, check whether the instance of this Activity exists in the BackStack. If so, end all other activities on this Activity in the BackStack.

In singleTask mode, the stack pressure and rollback operations of the Activity are as follows:


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.
SingleInstance:
The Activity marked as singleInstance Startup Mode starts a new BackStack when it is started. Only one Activity instance exists in this BackStack and this BackStack gets focus. This is a very extreme mode, which causes the entire device's operating system to have only one example of this Activity, no matter where it was started.

In singleInstance mode, the stack pressure and rollback operations of the Activity are as follows:

The call page of the Android system uses an Activity for multiple calls.
Of course, in Android, in addition to configuring the LauncherMode attribute in the AndroidManifest. xml configuration file, you can also set the Startup Mode in the code. To start an Activity in a component, you must use the startActivity () method. If you pass an Intent, you can use Intent. setFlags (int flags) is used to set the Startup Mode of the newly started Activity. The Code sets the Startup Mode of the Activity with a higher priority than AndroidManifest. configuration in the xml configuration file.
An integer data transmitted by the Intent. setFlag (int flags) method is set as a constant by the Android system:
· FLAG_ACTIVITY_NEW_TASK: This identifier will create a Task independently for the newly started Activity.
· FLAG_ACTIVITY_CLEAR_TOP, it is generally used in combination with FLAG_ACTIVITY_NEW_TASK.

· FLAG_ACTIVITY_SINGLE_TOP: equivalent to setting the LauncherMode attribute to singleTop.

Summary:

Standard is created every time, and each Task can have multiple instances (each Task can have multiple instances)
If the current singleTop instance is at the top of the stack, the OnNewIntent of the instance will not be created. If it is not at the top of the stack, a new instance is created (each Task can have multiple instances and can be reused at the top of the stack. Otherwise, an instance is created)
SingleTask creates a new Task. If another Task already exists and contains the instance, the instance of the Task is called directly. (Only one Task exists)
SingleInstance creates a new Task and has only one unique instance in the Task. (Only one Task exists, and only one Task exists)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.