Android in Launchmode detailed

Source: Internet
Author: User

The more time to do, the basic knowledge will forget the more clean, recently done a project, found that the activation of a few activities actually overlap, I ri~~, do not recall the Android industry will quit.

Conceptual Interpretation Task

Task is called tasks, this is simple, we need to complete the things, note, here we are talking about the task, is a noun, for example, to send a text message, then our task is sent a text message, this is no more, such as the instructor said: "Zhang San, you go to eat excrement!" "OK, that's three of the task is to eat excrement."

Back Stack

What do we call a fallback stack, or a task stack, what does that mean? As we said above, we need to complete the task, then we need to use a series of activity to complete, such as texting, the following steps are required to complete the task:

    • Open SMS Main Page mainactivity
    • Click the Add SMS button to open the Newsmsactivity
    • Write a text message in newsmsactivity and send

The above tasks involve two activities, then the two activity is stored in the back stack, and because it is the stack type of data structure, the above steps in this back stack are the following sequence:

    • Mainactivity First Press Stack
    • Click the Add button to newsmsactivity the stack,
    • SMS send complete, click Back button, Newsmsactivity stack, back to mainactivity
    • In Mainactivity Click the Back button, mainactivity the stack, when the back stack is empty, return to launcher

So we understand that this fallback stack is actually a container to store activity instances, and when each task is executed, a back stack is created, and the activity that is used in the task execution is pressed into the return stack in the order of Filo, When the task target is finished, when the return button is pressed, the activity in the back stack is stacked in the reverse order of the stack until there is no activity instance in the stack and enters launcher.

From this, we can also know that each task and back stack is a one by one corresponding relationship, in general, each need to execute a task, at least a back stack container, and the container will have at least one activity instance.

The role and use of Launchmode

In general, if there is no mental illness, Launchmode is used in the activity, we talk about the activity of the Launchmode

Launchmode effect

As the name implies, Launchmode is the boot mode, what is the startup mode? Startup mode means that after starting with a different mode, there will be different properties and performance, for example, Iron Man starts with normal mode, usually can kill all the soldiers in seconds, but if the Mad Hulk Hulk, you need to start the Super mode, put on anti-Hulk armor, or not hit, Put it in our actiivty here too, since it is configured on the activity, it shows that there are several startup modes of activity, and activity with different startup modes has different properties and performance.

So why do you need a startup mode? Demand! Yes, demand is the reason why everything is built or made, because we have different needs for actiivty, a cliché email example, email page activity requires no matter how open, how many times, there is only one instance of the page activity, right, If there are multiple instances we are very troublesome, do not know which to show, do not know which to close, then this is a requirement, corresponding to this demand, we need to set a startup mode for this homepage activity, no matter how open there is only one instance, so that the demand is satisfied.

Launchmode How to use

According to the instructions on Android developer, Launchmode has two places to use, one for mainfest activity node and one for flag in intent of startactivity method, The second approach we will discuss later, first of all to talk about the first method.

To tell the small white, launchmode to start the activity before the use of the effective, if the activity has been started, the instances are created, and then set what mode is useless.

The first way to use it is particularly simple, add android:launchmode to the Activity node in Mainfest, as follows:

<activity    android:name=".MainActivity"    android:label="邮件列表"    android:launchMode="singleTask"></activity>

There are four types of launchmode available, namely:

    • "Standard"
    • "Singletop"
    • "Singletask"
    • "SingleInstance"

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

According to the official Android developer, they can be instantiated in accordance with the activity of several times, the four patterns divided into two groups, "standard" and "Singletop" belong to the group can be instantiated several times, their instances can belong to any task, And can be anywhere on the back stack, the remaining two belong to the group that cannot be instantiated multiple times, they are often used to start a task, so there is only one instance of the task, and this is often at the very beginning of the back stack. This grouping helps us to understand the differences between each startup mode in a preliminary way.

Standard

Starting from now on the four types of boot mode, first look at "standard", sometimes referred to as the normal mode.

As we said earlier, activity defaults to this mode, so your activity settings and not set this is no different, so what is the performance of this model?

Suppose we have a "standard" mode of activity, the page has a button, click on this button will start the activity itself, because the setting is "standard" mode, each time you start the activity, A new instance of the activity is created and then placed in the back Stack, and a 100-time click creates 100 instances of the activity.

"Standard" is the simplest mode, but also in line with our normal thinking logic, so it is best to understand, I used the crude drawing tools to draw a picture, make a look at it:

Singletop

This startup mode differs little from the standard mode, only a little bit different.

We already know that each instance of activity is stored in the back stack, since it is a stack data structure, then the first instance of the stack is called a stack instance, because it will be pressed at the bottom of the next instance, and the last instance that is pressed in, called the stack top instance, because it has just been pressed into the stack, There is currently no other instance above it, if there is only one instance of the stack, then this instance is both a stack-bottom instance and a stack-top instance.

Understanding the concept of stack bottom and stack top, "singletop" to understand, when we start "singletop" mode of activity, the system will check the current back stack of the stack top instance is not the "singletop" mode activity instance, If so, no new instance is created, and the existing stack-top instance is reused directly. Take our last scene for example, if the activity is "singletop" mode, no matter how you click the button, the back stack will only have one instance, because there is already an instance of this at the top of the stack, so no new is created.

If the last example is not clear enough, we can give a more obvious example, there are two Activity:activitya is the standard mode, AVTIVITYB is "singletop" mode, complete a task, need to go through the following steps:

    • Start Activitya, click the button in Activitya to start Activityb,
    • Click the button in the Actiivtyb to start the activityb again

According to the previous description, when the instance of Activityb was first created, it was at the top of the stack, and the second attempt was made to create an instance of Activityb, because it was "singletop" mode, and the top of the stack already had an instance of it, it would no longer create a new one, after the task was completed, There is only one instance of Activitya in the back stack and an instance of Activityb, as shown in the following diagram:

Singletask

Next is the "Singletask" mode, remember the article at the beginning of the two groups and differences? If you remember now, then you understand the more than half.

Previously said, "Singletask" activity in a back stack will only create one, which is the biggest difference from the first two modes, "Singletask" and "singletop" difference is that when creating an instance is not only to check whether the top of the stack is already an instance , the entire back stack is also checked, so long as the back stack already exists, no new instances are created, either at the top of the stack, at the bottom of the stack, or anywhere else.

Not only does the "Singletask" mode not create a new instance, it clears all instances starting from the created instance to the top of the stack, and puts the created instance on top of the stack, because this is the only way to make the instance visible to the window.

To continue with the above example, the ACTIVITYC startup mode is "Singletask", and we insert a ACTIVITYC instance between the original Activitya and Activityb instances in the back stack to form such a back After the stack structure, if you try to start activityc again, because an instance of ACTIVITYC already exists, the instance that already exists is reused, and all instances of the instance to the top of the stack are cleared, so the instance of Activityb is cleared, and back There are only instances of Activtya and ACTIVTYC in the stack.

SingleInstance

"SingleInstance" is the last startup mode, and it is different from the other three modes.

The startup mode we talked about before is basically about whether we need to recreate the issue in the context of a back stack, where we'll expand the scope to discuss the re-creation between multiple back stacks, setting the "SingleInstance" The activty of the pattern, when started, is detached from the back stack of the current task and creates an instance in a new back stack.

As our previous example, we now put the instances of Activtya,activityb and activtyc into the back stack, and then make the back stack number 1th, and then write a activityd and set it to " SingleInstance "mode, at this point, if we start Activityd in Activityc, then the instance of Activiyd will not be in the 1th back stack, it will be in a new back A new Activityd instance is created in the stack with the following diagram:

Usage Scenarios

Different startup modes, suitable for different application scenarios.

Standard

The standard mode is suitable for most scenarios, because in the application, we can basically allow users to do multiple tasks at the same time, each task operates different data, which allows to create multiple instances of a activty, such as new mail activity, if a new message to Zhang San is currently being created, At this point, you also need to create a message to John Doe, at which point Activty will use standard mode, allowing multiple different instances to be created, allowing multiple messages to be created.

Singletop

Singletop mode, because of its characteristics is to check the top instance of the stack, you can use this feature to prevent a short time to create multiple instances, such as a button, click to open a video playback activty, if the user for a short time repeatedly click, not singletop mode, There will be multiple instances in the back stack for a short time, and the playback progress of each instance is inconsistent, and if it is singletop mode, no matter how many times it is opened.

Singletask

This mode, commonly used in those who have a certain task, and the task has been part of, but suddenly to do something else, and then come back to continue the task of the scene, or play that video example, if you need to open a new activty search related video, search finished and back to the playback page, Then this page is more suitable for using Singletask mode

SingleInstance

This mode is less used, if you must use this startup mode in your application, please consider in advance whether it is necessary, in addition, in the application of some special scenarios, such as launcher's main screen, may be used.

Android in Launchmode detailed

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.