How to correctly understand and use the four startup modes of activity

There are many articles about activity startup mode, but some articles are too simple, while others pay too much attention to details. This paper wants to take a compromise and only focus on the most important and commonly used concepts, principles and usage methods, so as to facilitate the correct application of readers.

There are four startup modes for activities: standard.singletop.singletask.singleinstance. You can specify the startup mode of each activity in the androidmainifest.xml file. An Android application generally has multiple activities. The system will manage these activities through the task stack. The stack is a last in first out collection. The current activity is at the top of the stack. Press the return key and the activity at the top of the stack will exit. Depending on the activity startup mode, the system manages activities through the task stack in different ways, which will be introduced below.

1 standard mode

Standard mode is the default startup mode of Android. If you do not make any settings in the configuration file, this activity is the standard mode. In this mode, an activity can have multiple instances. Each time you start an activity, whether there is an instance of this activity in the task stack or not, the system will create a new activity instance. The following is the experimental verification.

Create a new 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 to use standard mode? Standard 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, other modes should be considered.

2 singletop mode

Singletop mode is very similar to standard mode. The main difference is that when an activity in singletop mode is already at the top of the task stack, it will not create a new instance. If it is not at the top of the stack, it will create a new instance. Now change the startup mode of firstactivity in the configuration file to singletop. Our application has only one activity, Firstactivity is naturally at the top of the task stack.

After the application is started for the first time, we press the button to start a new firstactivity. It is found that the oncreate function is no longer printed in the log information, indicating that a new firstactivity instance is no longer created.

Here is a new question: how should we handle each time we start an activity. The answer is the onnewintent () function. Although the system will not call oncreat (), it will call onnewintent. We can do corresponding processing in this function.

When an activity is at the top of the stack, but it is still possible to start it, and you don't want to generate a new activity instance, you can use the singletop mode. For example, a search activity can enter search content or generate search results. At this time, the singletop mode can be used, and an instance will not be generated every time the user searches.

3 singletask mode

The activity in singletask mode 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 that in singletop mode. However, when an activity already exists but is not at the top of the stack, the system will move the activity to the top of the stack and take the activity above it out of the stack. Modify the above program, create a new secondactivity, set firstactivity to singletask startup mode, let it start secondactivity, and then let secondactivity start firstactivity.

Log information is as follows

When secondactivity starts firstactivity, oncreate function of firstactivity will not be called, but onnewintent function and ondestroy function of secondactivity will be called, and the secondactivity instance will be destroyed.

The biggest difference between the singletask mode and the previous two modes is that the singletask mode is a single instance within a task, so whether we set the activity to the singletask mode depends on whether our activity needs a single instance, such as an activity of yours

There is a list. If there are multiple instances, it may lead to inconsistent lists seen by users. Some activities need to be started frequently. If instances are created every time, it will occupy too many resources. In these cases, the singletask mode can be used, but starting an activity in singletask mode will destroy the activities above it in the task stack, It may affect the user experience. Pay attention to it when using it.

4 singleinstance mode

Singleinstance mode is also a singleton, but unlike singletask, singletask is only a singleton in the task stack. There can be multiple singletask activity instances in the system, while singleinstance activity has only one instance in the whole system. When starting a singleinstanceactivity, the system will create a new task stack, and the task stack has only one activity.

Singleinstance mode is not commonly used. If we set an activity to singleinstance mode, you will find that it starts slower, the switching effect is not good, and the user experience is affected. It is often used between multiple applications, such as an activity in a TV launcher. It can be started in any case through a key on the remote control, and the activity can be set to singleinstance mode. When you press the key to start the activity in an application and press the return key after processing, it will return to the previous application, which will not affect the user experience.

The four startup modes of activity are analyzed above. There is no standard answer to which startup mode the activity is set. Sometimes, you may find that there is no obvious difference between setting an activity as one startup mode or another startup mode, and the specific evaluation criterion is to see which mode is more suitable for application functions and more conducive to user experience.

The above is the whole content of this article. I hope the content of this article can bring some help to your study or work. At the same time, I also hope to support a lot of programming tips!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>