How to correctly understand and use the four startup modes of Activity, activity4

Source: Internet
Author: User

How to correctly understand and use the four startup modes of Activity, activity4

There are already many articles about the Activity startup mode, but some articles are too simple and some are too detailed. This article wants to take a compromise and focus only on the most important and most commonly used concepts, principles and usage to facilitate the reader's correct application.

There are four activation modes for an Activity: standard. singleTop. SingleTask. singleInstance. You can specify the Startup Mode for each Activity in the AndroidMainifest. xml file. An Android application usually has multiple activities. The system manages these activities through the task stack. the stack is a collection of first-in-first-out activities. The current Activity is at the top of the stack and press the return key, the Activity at the top of the stack exits. The system uses the task stack to manage the Activity in different startup modes.

1 Standard mode

Standard mode is the default startup mode of Android. If you do not set any settings in the configuration file, this Activity is the standard mode. In this mode, the Activity can have multiple instances and start the Activity each time, the system creates a new Activity instance no matter whether an instance of this Activity exists in the task stack. The following is an experiment verification.

Create a FirstActivity and start it with a Button:

 

It is found that a new FristActivity will be started every time. The Log information is as follows:

 

When should we use the standard mode? Standartd mode is the default mode of activity. In most cases, this mode should be used, that is, nothing needs to be done in the configuration file. When there are special requirements, consider other models.

2 SingleTop Mode

The SingleTop mode is very similar to the standard mode. The main difference is that when a singleTop Activity is located at the top of the stack of the task stack and starts it, no new instances will be created, if it is not at the top of the stack, a new instance will be created. Now, the startup mode of FirstActivity in the configuration file is changed to SingleTop. Our application only has one Activity, firstActivity is naturally at the top of the stack.

After the application is started for the first time, press the Button to start the new FirstActivity. It is found that the onCreate function is no longer printed in the Log information, indicating that no new FirstActivity instance is created.

There is a new problem. How can we handle each Activity separately. The answer is the onNewIntent () function. Although the system will not call onCreat (), it will call onNewIntent. We can perform corresponding processing in this function.

 

When an Activity is already on the top of the stack, but it is still possible to start it, and you do not want to generate a new Activity instance, you can use the singleTop mode. For example, you can enter the search content or generate search results for a search Activity. In this case, you can use singleTop mode. No instance is generated for each search.

3 SingleTask Mode

The SingleTask mode Activity has only one instance in the same Task. if the Activity is already at the top of the stack, the system will not create a new Activity instance, which is the same as the singleTop mode. However, if an Activity already exists but is not at the top of the stack, the system moves the Activity to the top of the stack and outputs the activity above the stack. Modify the above program, create a new SecondActivity, set FirstActivity to singleTask startup mode, enable it to start SecondActivity, and then let SecondActivity start FirstActivity.

The Log information is as follows:

 

When SecondActivity starts FirstActivity, it does not call the onCreate function of FirstActivity, but calls the onNewIntent function and the onDestroy function of SecondActivity. The SecondActivity instance is destroyed.

The biggest difference between the singleTask mode and the previous two modes is that the singleTask mode is the singleTask mode, so whether to set the Activity to the singleTask mode is to check whether the activity requires a singleTask, for example, one of your activities

There is a list in it. If there are multiple instances, the list that the user may see may be inconsistent. Some Activity needs to be started frequently. If an instance is created every time, it will occupy too much resources, in these cases, you can use singleTask mode. However, if you start a singleTask Activity, the Activity on the task stack is destroyed, which may affect the user experience.

4 SingleInstance Mode

SingleInstance mode is also singleTask, but unlike singleTask, singleTask is only a singleTask in the task stack. The system can have multiple singleTask Activity instances, singleInstance Activity has only one instance in the system. When a singleInstanceActivity is started, the system creates a new job stack, and this job Stack has only one Activity.

The SingleInstance mode is not commonly used. If we set an Activity to the singleInstance mode, you will find that it is slower at startup, and the switching effect is poor, affecting the user experience. It is often used between multiple applications. For example, an Activity in a TV launcher can be started using a key in the remote control. This Activity can be set to singleInstance mode, when you press the button in an application to start the Activity, and then press the return key, the application will be started before the operation, without affecting the user experience.

The above analysis shows the four startup modes of the Activity. There is no standard answer to set the Startup Mode of the Activity. Sometimes, you may find that there is no obvious difference between setting an Activity as a startup mode or another startup mode. The specific criterion is to determine which mode is more suitable for application functions, it is more conducive to user experience.

Reprinted please indicate the source Zhuo Walker blog http://www.cnblogs.com/thewalker3000/p/6546467.html

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.