Four launchmodes of Android notes

Source: Internet
Author: User

I. First come to understandTask

Task, as its name implies, creates and maintains a Task of its own when every Android application is running. In fact, a Task is a container containing the stack structure, this stack is usually called a rollback stack. It is used to save the window objects created in all the current Android applications. Generally, we can see the window objects at the top of the rollback stack. When we open a new interface, the previous interface (window object) will be pushed into the stack, giving the top position of the stack to the new interface (window object ); when the window object is closed, the system will first pop up the object stack and destroy the object. When the last window object in the stack is popped up, the rollback stack is empty. At this time, the rollback stack is destroyed and the application is stopped. (It should be noted that at this time, the application does not completely exit the memory, and some of its resources, such as static variables, will remain in the memory for a long time .)

Note that,Each application has a uniqueTaskWhen a new application is opened, its task appears at the front end and is displayed to the user. The old task is transferred to the background, at this time, the system will automatically save the status of the old task window object. After the task is re-switched to the foreground, the window will return to the focus and restore all the statuses, however. Do not think that tasks in the background will stay in the memory until you come back. When the phone memory is exhausted or a new APP requires a large amount of memory, you will wait until your old task is ruthlessly killed by the system, the longer the task is stopped, the sooner it is destroyed.

Ii. Four startup ModesLaunchMode

Activity has four loading modes:

  • Standard
    • SingleTop
    • SingleTask
      • SingleInstance

        The set location is in the android: launchMode attribute of the activity element in the AndroidManifest. xml file:

        Mode

        Description

        Standard

        The default value of launchMode. This mode is already used when a startActivity is created without being written by default. Every time a startActivity is called, the system will instantiate a new window object, there is one more window object for this instance in the rollback stack. Unless you press the back key or call the finish () method in the Code, the Operation persists until the application stops. A new instance is generated no matter whether there are existing instances.

        SingleTop

        If the window instance is on the current interface (that is, the stack top of the rollback stack), the system will not create new window objects (the standard mode is different, whether it is the top stack or not, it will be created). Use the window instance directly. The advantage of doing so is that as long as the instance to be used is always on the interface, there is only one instance in the rollback stack, and the instance can be exited directly by pressing it once (because there is only one window object ), this mode is especially suitable for NFC.

        If a corresponding Activity instance is found at the top of the stack, it will be reused and will not be regenerated into a new instance. However, note that,It only looks at the top of the stack, not in the stackIf the instance does not exist at the top of the stack, it also exists in the stack, and it will still create a window object instance.

        SingleTask

        This mode is more powerful and overbearing, that is, if a corresponding Activity instance is foundAll other Activity instances on the Activity instance go out of the stack.To make the Activity instance the top object of the stack, which is displayed on the current interface without creating a new instance. If the window instance does not exist, it is created and placed on the top of the stack.

        SingleInstance

        This mode is more exclusive. If the current rollback stack exists, all the previous instance objects are cleared and placed on the top of the stack. If the current rollback stack does not exist, it creates a rollback stack, creates an instance object, and does not allow other instance objects to come in. This mode is rarely used, because creating a new rollback stack for only one instance object is too memory-consuming. It is of little significance.

        The above text is boring and there are no specific examples. The reason for not writing an example is that some predecessors have already written a detailed analysis instance. If you still don't understand it, read this blog. Http://blog.csdn.net/liuhe688/article/details/6754323

        In addition, upload a routine dedicated to describing four different modes. This is an example in the Android Development Authority guide. You can run it to see if this is the case.


        Download routine


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.