In-depth analysis of the Activity Startup Mode and in-depth analysis of the activity
There are many articles on the Activity Startup Mode on the Internet, but they all look the same. After reading these articles, we can all understand these four startup modes. However, the official api has some controversy about the singleTask startup mode, as a result, I did not really understand these modes, especially how to use them in actual development. So I spent a day studying these four startup modes.
First, we should know the concept of Task. It is an activity stack formed by a series of operations to jump to the activity in a coherent manner. Later, first-in-first-out means that the activity we see is at the top, the concept of Task is quite clear in a blog, which is introduced by Lao Luo's blog:
"Before solving this mystery, let's take a look at the concept of a Task in an Android Application. We know that Activity is one of the basic components of an Android application. When an application is running, each Activity represents a user operation. A series of operations performed by a user to complete a function form an Activity sequence, which is called a task in Android applications. It is based on the user experience, abstract The concept of organizing a group of related activities together."
Why should we set different startup modes? The purpose is to better reuse the activity. There are two benefits: saving system resources and avoiding bad user experience. For example, the activity already exists, except for the standard mode, the other three modes are used to check whether the activity exists. If the activity exists, the onCreate () method is no longer executed. Instead, the activity is directly displayed on the top of the stack and onNewIntent () is called (). The difference between singleTop singleTask singleInstance is that the search range is different. The minimum value of singleTop is only at the top of the stack of the current task (single application level ), however, singleTask not only searches for the current task but also other tasks (semi-application and semi-system level), while singleInstance only searches for other tasks, because the activity with singleInstance exclusively occupies one Task, your current task cannot contain this activity! In a broad sense, we can also consider singleInstance as a global system!
Therefore, we can better understand the four startup modes:
Standard is not limited to the number of instances.As long as the task is started, a new instance is created at the top of the current Task, which can be superimposed. Note that it does not create a new task.
SingleTop has only one instance at the Top of the current stack.For example, you can only start the same instance, but this requirement seems to be rare. Another kind of requirement is that the interface is opened and started repeatedly by the external service or broadcast, for example, the news client opens the interface for receiving push. If you open activityB of another application in your acticityA, setting this B to singleTop is meaningless because it only judges the current Task of, so I understood it as a single application level. (Of course, more rigorous, only activity. Because service or broadcast can be of other applications)
SingleTask has only one instance in all tasks.This startup mode is controversial at most, especially the official documentation said: "singleTask" will run in the new task and is located at the bottom of the task stack. Many people do not agree, and Luo also wrote a blog to explain it. In fact, the official documentation is correct, because singleTask is generally used with android. intent. action. MAIN and android. intent. category. LAUNCHER is configured together. As the program entry, it is of course the bottom of the stack. Starting a new Task means that the current Task does not have this activity, and the correct use of singleTask should satisfy this situation, therefore, the explanation of the official website document is based on the correct use of this attribute. If you do not set singleTask and android. intent. action. main category. the LAUNCHER is configured together, which is another argument but meaningless.
So let's first talk about the startup logic of singleTask: 1. If the current task has this activity, we will directly clear all the above activities, and it will be displayed. 2. if task A does not have this activity, but other task B (generally the task of another application), we will directly move task B to task, and clear the unnecessary activity on task B. Of course, this activity is not needed if it is at the top of task B, press the rollback key to roll back from task B to task A one by one. if no task has this activity, A new instance is added to task B and placed on task ). According to this logic, we set the browser's homepage to singleTask and added the Main and LAUNCHER attributes. When we started it, it was at the bottom of the stack. After a while, the browser caused a lot of activity accumulation, we only need to go back to the home page, because it is a singleTask, and the above activities are all clear. At this time, the user will exit the entire application after clicking back, which is very comfortable and user-friendly. In addition, if we want to access a webpage from other applications, we will open the browser homepage, because it will certainly return to our own application after we have accessed the website at the bottom of the stack, imagine that the activity on this homepage is not associated with Main & LAUNCH, and there must be other activities in the browser stacked below. After the visit, we will see the activity not closed before the browser, it is strange that the system returns only after multiple retreats, isn't it. In addition, the browser homepage is a heavy interface. If it is not set as a singleTask, we need to open many new pages from other applications, which is a waste of resources. The same applies to the home page of the contact APP.
SingleInstanceThere is only one instance for all tasks and only one activity for this Task exists.This is rarely used. For example, in the InCallUi interface on which the phone is answered, It is put into a single task and does not coexist with any other activity because it is unnecessary, what else can I do on the phone answering interface? For example, the alert page for an alert when an alert is triggered is an independent activity that is not associated with other activities and does not need to be redirected.
To sum up, you can combine the first two modes for learning. Generally, in a single application development, this interface will not be enabled by other applications, and singleTop is frequently used for redirection, it is not easy to jump and the activity is less standard. For system-level applications such as browsers, contacts, telephones, and text messages, consider the last two modes. For application scenarios written by others, refer:
SingleTop is applicable to the content display page for receiving notifications. For example, it is annoying to open a page of news content on a news client every time if 10 news pushes are received. SingleTask can be used as a program entry point. For example, the main interface of the browser. No matter how many applications start the browser, only the main interface is started once. In other cases, the onNewIntent is used and other pages on the main interface are cleared. SingleInstance is suitable for pages that need to be separated from programs. For example, you can separate the alarm and alarm settings. SingleInstance is not used for intermediate pages. If it is used for intermediate pages, the jump may be faulty, for example, A-> B (singleInstance)-> C. After it is completely exited, It is started here, B is enabled first.
Reference: http://blog.csdn.net/luoshengyang/article/details/6714543
Https://developer.android.com/guide/components/tasks-and-back-stack.html