Android activity Four big start mode detailed

Source: Internet
Author: User

Four startup modes in activity

In Androidmanifest.xml, there is a default activity in which you can set the activity startup mode, android:launchmode= "", which is used to configure the load mode of the activity. This property supports 4 properties in each of the different modes with different effects, and the following is a detailed start mode.

Standard: Normal mode, default load mode

Singletop:task Top Single case mode
Singletask:task in single-case mode
SingleInstance: Global Singleton mode


1 Why should the activity be in the specified mode??

Let's start with Android's management of activity: Android uses task to manage multiple activity, and when we start an activity, the system creates a task and then launches the activity's entry.
Android does not provide an API to the task, only by invoking the activity's GetTaskID () method to get the ID of the task it is in, we can interpret the task as the activity stack and the task to manage the activity on the stack.


2. Understanding of four starting modes


Standard load Mode :

Each time the activity is started in this mode, Android will always create a new instance of the activity that is launched and add that activity to the current task stack, which does not create a new task, but adds the new
Activity is added to the original task


Singletop mode


If there is an instance of the activity at the top of the task, reusing the instance will create a new instance and put it in the top of the stack (even if the activity instance already exists on the stack, as long as it is not on top of the stack, an instance will be created).


Singletask mode


The activated activity has only one instance of activity within the same task, and is divided into the following three scenarios:


<1> If the target activity that is started does not exist in the task stack, the system will create a target activity instance and add it to the top of the task stack


<2>: If the target activity that you started already exists at the top of the task stack, the mode and singletop mode are the same


<3>: If the target activity that is started already exists but is not at the top of the task stack, the system will remove all activity above the target activity to the task stack, so that activity is placed on top of the task stack


SingleInstance mode

In this load mode, a target activity instance is created, regardless of which task is started, and a new task stack is used to mount the activity instance. It can be divided into two kinds of situations:


<1> If you create a target activity that does not exist, the system creates a completely new task, creates an activity instance, and then adds the target activity to the top of the new task stack.


<2> If the created target activity already exists, the system will place the activity's stack in the foreground, regardless of the task stack.


Note: Activity with singleinstance load mode is always at the top of the task stack, and the activity's task stack contains only that activity.



3. Four Boot mode examples


Standard

Where standard is the default startup mode for the system.

The following example illustrates the operating mechanism of standard:

Private Button Btn_mode;   @Override public   void OnCreate (Bundle savedinstancestate) {       super.oncreate (savedinstancestate);       Setcontentview (r.layout.activity_main);       Text_show = (TextView) This.findviewbyid (r.id.text_show);       Text_show.settext (This.tostring ());       Btn_mode = (Button) This.findviewbyid (R.id.btn_mode);   } button click event public   void Launchstandard (View v) {       startactivity (new Intent (This,mainactivity.class));       

The initialization interface is as follows:


When the button is clicked, a new activity is created, which can be seen by the display of the 16-digit number after [email protected], and two times the interface is as follows:

at this point, we analyze the operating mechanism inside the stack: (from the top of the stack in turn )

Therefore, this standard mode creates a new activity object each time, and when you click the Back button, he destroys the top of the stack (the current activity) and jumps to the next level, for example, if the activity is 44ED8C50 now, Then when we click Back, the activity changes to 44f28a48, but when you click the button again in this activity to create the object, it creates a new activity object, which is probably not what we need in most cases, because it consumes too much of the system's performance.


Singletop


As you can see from the above explanation, the current activity at the top of the stack is automatically detected each time a new activity is used, whether it is an activity that needs to be referenced, or if it is directly referenced, without creating a new activity.


We added a "Start singletop mode" button in the interface just now, when we clicked, we created the singletop, there was a button in activity singletop to start the singletop mode, which means to start the current activity, Since we configured the activity's startup mode in the manifest file to be singletop, we will not create this at this time but take advantage of the singletop activity at the top of the current stack:

<activity             android:name= ". Singletopactivity "             android:label=" @string/singletop "             android:launchmode=" Singletop "> </activity >


The initialization interface is as follows:

When you click the "Start singletop Mode" button


We analyze its operating mechanism, we know that when the program runs to this point, the data form in the stack is:

when we click on the "Start Singletop Mode" button in the upper interface, because this activity set startup mode is Singletop, so it first detects whether the current stack top is the activity object that we want to request , has been verified,  So it does not create new activity, but refers to the activity at the top of the current stack.

Although it does not create a new activity object, it uses the Onnewintent () method for each callback:

@Override      protected void onnewintent (Intent Intent) {         //TODO auto-generated method stub         super.onnewintent (intent);         Toast.maketext (This, new Date (). toString (), 1). Show ();     

We write the code for this method to output the current date, and the current date is output each time you click the button above.


Singletask


This startup mode and Singletop in the name can see the difference, that is, singletop only detect the current stack at the top of the activity is we need to request to create, and Singletask will detect the stack of all activity objects, from the top down, If it detects what we are requesting, it will destroy the object above the activity object and put the detected activity to the top of the stack directly.

We create a singletaskactivity, which contains a launch mainactivity and launch Singletaskactivity button.


Initialization

When you click on the "Start Singletop mode" button:


Click on the second button "Launch Singletask mode" button in this interface, according to the definition will detect whether the activity object in the current stack , so the current activity is displayed, will not be recreated;


Then click on the "Start Standard mode" button, since Mainactivity's boot mode is standard, a Mainactivity object will be recreated here:


the sort order data format in this stack is:


when the "Start Singletask Mode" button is clicked in the upper interface, the topmost mainactivity will be wiped out due to the detection of the second activity that we are creating in the current stack. Then set the singletaskactivity to the top of the stack :

singleinstance


This startup mode works similarly to the browsers we use, and we all know that when accessing a browser in multiple programs, if the current browser is not open, the browser opens, otherwise it will be accessed in the currently open browser . This mode saves a lot of system resources because he can guarantee that the activity object to be requested only exists in the current stack.


There are four startup modes in Android, which we often use when developing Android projects, which can save system overhead and program run efficiency by cleverly setting the activity's startup mode.


Android activity Four big start mode 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.