Detailed introduction to four startup modes of Android launchmode

Detailed explanation of Android launchmode

The longer you do it, the cleaner you forget the basic knowledge. In a recent project, I found that several activities started overlap. I RI ~ ~, and I'm going to quit the Android industry if I don't recall it.

Conceptual interpretation

Task

Task is called task, which is simple and means what we need to accomplish. Note that here we are talking about task, which is a noun. For example, if we want to send a text message, our task is to send a text message, that's all. For example, the instructor says, "Zhang San, you eat shit!", OK, that three's job is to eat shit.

Back Stack

We often call it fallback stack, or task stack. What does this mean? As mentioned above, if we need to complete the task, we need to use a series of activities to complete it. For example, if we send text messages, the following steps are required to complete the task:

The above tasks involve two activities, which are stored in the back stack. Because the back stack is a stack type data structure, the activity sequence of the above steps in the back stack is as follows:

Therefore, we understand that the fallback stack is actually a container for storing activity instances. When executing each task, first create a back stack, and press the activities used in the task execution process into the back stack in Filo order. After the task target is completed, press the return button, The activities in the back stack pop the stack in the reverse order of pressing the stack until there is no activity instance in the stack and enter the launcher.

Therefore, we can also know that each task and back stack are one-to-one correspondence. Generally, every time a task needs to be executed, at least one back stack container is required, and there will be at least one activity instance in this container.

Function and usage of launchmode

In general, if there is no psychosis, launchmode is used on activity. Let's talk about launchmode on activity

Launchmode function

As the name suggests, launchmode is the startup mode. What is the startup mode? Starting mode means that after starting in different modes, there will be different attributes and performances. For example, iron man starts in normal mode, which can generally kill all small soldiers and generals. However, if you deal with the crazy Hulk, you need to start super mode and wear anti Hulk armor. Otherwise, you can't beat it. The same is true for our actiivty, Since it is configured on the activity, it means that the activity has several startup modes, and the activities started with different startup modes have different properties and performances.

So why do you need startup mode? Demand! Yes, requirements are the reason why everything is created or manufactured, because we have different requirements for actiivy. For example, the email home page activity requires that no matter how it is opened and how many times it is opened, there can only be one instance of the home page activity. Right? If there are multiple instances, we are in trouble and don't know which one to display, I don't know which one to close. This is a requirement. Corresponding to this requirement, we need to set a startup mode for the home page activity. No matter how we open it, there will only be one instance, so the requirement can be met.

Launchmode usage

According to the instructions on Android Developer, launchmode has two places to use. One is under the activity node of mainfest, and the other is to set flag in the intent of startactivity method. The second method will be discussed later. Let's talk about the first method first.

What I want to tell Xiaobai is that the launchmode is valid only before starting the activity. If all activities have been started and all instances have been created, it is useless to set any mode.

The first method is particularly simple. You can add Android: launchmode to the activity node in mainfest, as shown below:

There are four launchmodes available:

If you do not add a launchmode, the default launchmode is "standard".

According to the official Android Developer, they divide the four modes into two groups according to whether the activity can be instantiated multiple times. The "standard" and "singletop" belong to the group that can be instantiated multiple times. Their instances can belong to any task and can be located anywhere on the back stack. The other two belong to the group that cannot be instantiated multiple times, They are often used to start a task, so there is only one such instance of a task, and this is often at the beginning of the back stack. This grouping can help us preliminarily understand the differences between each startup mode.

standard

From now on, let's explain these four startup modes. First, let's take a look at "standard", sometimes called standard mode.

As we said earlier, activity is in this mode by default, so there is no difference between setting your activity and not setting it. What is the performance of this mode?

Suppose we have an activity in "standard" mode, and there is a button on the page. Clicking this button will start the activity itself. Because the "standard" mode is set, each time we start the activity, we will create a new instance of the activity and put it into the back stack in turn. Click 100 times to create 100 instances of the activity.

"Standard" is the simplest model, which is also in line with our normal thinking logic, so it's best to understand. I drew a picture with a simple drawing tool and made do with it:

singleTop

This startup mode is not different from the standard mode, only a little different.

We already know that the instance of each activity is stored in the back stack. Since it is a stack data structure, the first instance of pressing the stack is called the bottom instance because it will be pressed at the bottom by the later instance. The last instance pressed in is called the top instance because it has just been pressed into the stack and there are no other instances on it for the time being, If there is only one instance in the stack, this instance is both the bottom instance and the top instance.

When we understand the concepts of bottom and top of stack, "singletop" is easy to understand. When we start the activity in "singletop" mode, the system will check whether the top instance of the current back stack is an instance of the activity in "singletop" mode. If so, we will not create a new instance and reuse the existing top instance directly. Take our previous scenario as an example. If the activity is in the "singletop" mode, no matter how you click the button, there will only be one instance in the back stack. Because there is already one such instance at the top of the stack, no new one will be created.

If the previous example is not clear enough, we can give an obvious example. There are two activities: activitya is the standard mode and avitivityb is the "singletop" mode. To complete a task, you need to go through the following steps:

According to the previous description, when an instance of activityb is created for the first time, it is at the top of the stack. Before the second attempt to create an instance of activityb, because it is in the "singletop" mode and there are already instances at the top of the stack, no new instances will be created. After the task is completed, there is only one instance of activitya and one instance of activityb in the back stack. The diagram is as follows:

singleTask

Next is the "single task" mode. Remember the two groups and differences mentioned at the beginning of the article? If you remember now, you understand more than half of it.

As mentioned earlier, the activity of "singletask" will only create one in a back stack, which is the biggest difference from the first two modes. The difference between "singletask" and "singletop" is that when creating an instance, it will not only check whether there are instances at the top of the stack, but also check the whole back stack. As long as the instance of back stack already exists, whether it is at the top of the stack, No new instance will be created at the bottom of the stack or anywhere.

The "singletask" mode not only does not create a new instance, but also clears all instances from the created instance to the top of the stack, and places the created instance on the top of the stack, because only in this way can the instance be displayed on the window.

Continue with the above example. The startup mode of activityc is "singletask". We insert an instance of activityc between the original activitya and activityb instances in the back stack to form such a back stack structure. If we try to start activityc again, the existing instances of activityc already exist, so we reuse the existing instances, And all instances from the instance to the top of the stack are cleared, so the instances of activityb are cleared. At this time, there are only instances of activitya and activityc in the back stack.

singleInstance

"Singleinstance" is the last startup mode, which is different from the other three modes.

The startup mode we talked about before basically talks about whether it needs to be re created within a back stack. Here, we expand the scope to discuss the re creation between multiple back stacks. We set the activity of "singleinstance" mode, which will be separated from the back stack of the current task when starting, Create an instance in a new back stack.

As in our previous example, now put the instances of activitya, activityb and activityc into the back stack in turn, number the back stack as No. 1, and then write an activityd and set it to the "singleinstance" mode. At this time, if we start activityd in activityc, the instances of activiyd will not be located in the No. 1 back stack, It will create a new activityd instance in a new back stack, as shown below:

Usage scenario

Different startup modes are applicable to different application scenarios in the application.

standard

The standard mode is applicable to most scenarios, because in the application, we can basically allow users to perform multiple tasks at the same time, and each task operates different data. This allows you to create multiple instances of an activity, such as a new mail activity. If you are currently creating a new email to Zhang San, you need to create an email to Li Si at the same time, At this time, the activity will use the standard mode, which allows the creation of multiple different instances and multiple messages.

singleTop

Singletop mode is characterized by checking the instances at the top of the stack. This feature can be used to prevent multiple instances from being created in a short time. For example, there is a button to open an activity to play video after clicking. If the user clicks repeatedly in a short time, multiple instances will appear in the back stack in a short time, and the playback progress of each instance is inconsistent, If it is in singletop mode, no matter how many times it is opened, it has no effect.

singleTask

This mode is often used in scenes where there are certain tasks and some of the tasks have been carried out, but they suddenly do other things. When they come back later, they have to continue the task. It is still an example of playing the video. If they need to open a new activity to search for relevant videos while playing, they will return to the playing page after the search, Then this page is more suitable for using the single task mode

singleInstance

This mode is rarely used. If you must use this startup mode in your application, please consider whether it is necessary in advance. In addition, it may be used in some applications under special scenarios, such as the main screen of launcher.

Thank you for reading, hope to help you, thank you for your support to this site!

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
分享
二维码
< <上一篇
下一篇>>