In-depth analysis of activity startup mode

Source: Internet
Author: User

There are a lot of articles on the activity startup mode on the Web, but they all look the same, and we can understand these 4 startup modes after reading, but the official API has some controversy about Singletask's startup mode. Cause I actually did not really understand these patterns, especially to the actual development of how to use or smattering, so spent a day to study these 4 startup mode.

The first should know the concept of the task, it is our series of operations coherent jump activity formed an activity stack, LIFO that is the current activity in the top, about the concept of the task of Lao Luo a blog is relatively clear, quoted from the Lao Luo blog:

"Before we solve this puzzle, let's take a quick look at what a task is like in an Android application." We know that activity is one of the fundamental components of an Android application, and each activity represents a user action when the application is running. The sequence of actions a user performs in order to complete a function is a sequence of activity that is called a task in an Android application, and it is an abstraction from the perspective of a user experience that organizes a set of related activity together. ”

          Why do you want to set a different startup mode? is to better reuse activity, bring two benefits one is to save system resources, and to avoid bad user experience, such as the current activity already exists, in addition to standard three other modes will go to find whether there is activity, If there is no longer executing oncreate () go to new instead and simply show the activity to the top of the stack and call Onnewintent (). Singletop  singletask singleinstance The difference is that the scope of the search is not the same, singletop minimum will only be seen on the top of the current task stack (single application level), and singletask not only in the current task but also in other task lookups (semi-system level), and singleinstance will only go to other task lookup, because there is singleinstance is exclusively a task, and your current task cannot contain this activity! In the broadest sense, singleinstance is a global system!

So you can understand the 4 startup modes more clearly:

Standard Unlimited number of instances. as soon as the start is on the top of the current task new instance, can overlay, note that it does not create a new task


singletop There is only one instance of the top (top) of the current stack. For example, self-priming will only be the same instance, but this kind of demand seems to be very small, there is a interface open, by external service or broadcast repeatedly started, such as news client open to accept push open interface. Then you open another application in your own Acticitya activityb, this b is set to singletop is meaningless, because it only judge the current a task,so I understand it as a single application level. (Of course, a bit more rigorous only activity because the service or broadcast can be other applications)


Singletask has only one instance of all tasks . This startup mode controversy most especially official documents say: "Singletask" will run in a new task and be at the bottom of the task stack. Many people do not agree, Lao Luo also wrote a blog to explain, in fact, the official document is correct, Because singletask generally with Android.intent.action.MAIN and Android.intent.category.LAUNCHER configuration, as a program entrance he is, of course , the bottom of the stack, Will be in the new task start refers to the current task does not have this activity, and the correct use of singletask should satisfy this situation , so the official website of the interpretation of the document is based on our correct use of this property, If you don't put Singletask and Android.intent.action.Main category. Launcher configuration together, that is another but meaningless.

            So first singletask start logic:1.  If the current task has this activity, we'll just put all the activity clear on it and it will show up. 2.  If the current task a is not present but the other task b (typically other application's task) has this activity, we move the task b to the Task a directly, and the task  b above the excess activity clear off, of course, if the activity on the top of the task b is not necessary, moved over the show, at this time press the fallback button will start from Task b to task  a fallback   3.  If none of the tasks are in this activity, the new instance will be placed on the Task a (note that this task is new task b). According to this logic, we set the homepage of the browser to Singletask and add the properties of main and launcher, a start it is at the bottom of the stack, with a period of time the browser caused a lot of activity accumulation, we just go back to the homepage, Because it is singletask then the above activity is clear, at this time the user points back to exit the entire application, very comfortable user-friendly operation. Also, we from other applications such as want to access the Web page will open this browser homepage, because it must be at the bottom of the end of our visit to go back to our own application, imagine this home activity does not and main&launch together, Then he must have a browser. Other activity stacks, we have visited the point back to see the browser did not close the activity, many times back to return, very strange not. and the browser homepage is a cumbersome interface, if not set to Singletask we open from other applications will have a lot of new, wasting resources. Including the Contact App home page is the same reason.

SingleInstance has only one instance in all tasks, and this task only has one activity present. This is less useful, for example, in the interface to answer the phone incallui, put him alone in a task, do not coexist with any other activity, because there is no need to answer the phone interface where can jump? For example, the Alarm clock alert interface, are not related to other activity without jumping interface, that is, the activity of strong independence.


Summary: You can put the first two models together to learn, generally in a single application development, the interface will not be enabled by other applications, and jump frequently with singletop, not jump and less activity with the standard can also. If the system level such as browser, contact person, phone, SMS will consider the latter two modes, others write the application scenario, refer to:

Singletop is suitable for receiving notifications to launch the content Display page. For example, a news client's news content page, if it receives 10 news feeds, is annoying to open a news content page each time.Singletask is suitable as a program entry point. For example, the browser's main interface. Regardless of how many apps launch the browser, only the main interface is started once, the rest will go onnewintent, and the other pages above the main interface will be emptied.the singleinstance is suitable for pages that need to be separated from the program. such as alarm reminders, separate alarm alarms from alarm settings. SingleInstance do not use in the middle page, if used in the intermediate page, jump transfer problems, such as: A-B (singleinstance)-C, after the full exit, this start, first opened is B.

Reference Document: http://blog.csdn.net/luoshengyang/article/details/6714543

Https://developer.android.com/guide/components/tasks-and-back-stack.html

In-depth analysis of activity startup mode

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.