Four kinds of launchmode for Android activity

Source: Internet
Author: User

Originally wanted to focus on the activity in the startup mode to write an article, and later on-line found that someone has summed up quite good, in this direct reference, and add some of their own understanding, here thank the original author.

Article Address:

http://blog.csdn.net/liuhe688/article/details/6754323

First, Introduction

The Launchmode has four modes that determine whether activity is regenerated at runtime, if the activity instance is re-generated, or if an activity instance already exists, or a task is shared with other activity instances. Here is a task stack concept, here first, this task stack is a stack structure of the container for storing activity instances, when the application starts, the system will create a task task stack to place the root activity instance. By default, when one activity launches another activity, two activity instances are placed on the same task stack, which is pressed into the task stack, and when the Back button is clicked, the last activity instance into the task stack is rolled back, The next activity instance is displayed in front of the screen, and the task and task are independent from each other.

Ii. Launchmode four models of activity
    • Standard
    • Singletop
    • Singletask
    • SingleInstance
1. Standard mode, also the default mode for activity:

When an activity initiates another activity, two activity instances are pressed into a task stack, and if the same activity (class) is started, a new activity instance is generated and added to the task stack.

2. Singletop

In an activity, configure the Launchmode=singletop property. When an activity launches another acitivty, it checks the task stack first, whether the activity instance at the top of the current stack is the activity instance to start, and the activity that is about to start at the top of the stack, reusing the current activity instance. Conversely, in the top of the stack is not going to start the activity instance, then regenerate an activity instance, press into the top of the stack.

PS: It is important to note that the activity that will be started, at the top of the current task stack , is thelife cycle of the activity when reusing the current activity instance:

    1. OnPause ();
    2. Onresume ();

Based on the above, for <activity> specify the attribute android:launchmode= "Singletop", the system will follow the Singletop startup mode to handle the jump behavior. We repeat the above several actions, will appear the following phenomenon:

          

We see this result is different from standard, three serial numbers are the same, that is, using the same firstactivity instance; If you press the back key, the program exits immediately, indicating that there is only one activity instance in the current stack structure. The principle of Singletop mode is as follows:

      

  

As shown, the system will first look in the stack structure to see if there is a firstactivity instance on the top of the stack, and if there is no new one, use it directly. Perhaps friends will have doubts, I only see the stack only one activity, if it is a number of activity what to do, if not at the top of the stack? We will then pass an example to confirm the question.

Let's create a new activity named Secondactivity, as follows:

      

We see that the serial numbers of the two firstactivity are different, proving that a new firstactivity instance was generated when jumping from secondactivity to firstactivity. Schematic diagram is as follows:

      

  

We see that when jumping from secondactivity to firstactivity, the system discovers that there are firstactivity instances, but not at the top of the stack, and then regenerates an instance.

This is the Singletop startup mode, and if a corresponding activity instance is found to be at the top of the stack, it is reused and no new instances are generated.

3. Singletask

Configuring the activity's launchmode=singletask, this activity creates a new task stack to store the activity instance. When an activity initiates another activity (the activity's launchmode=singletask), it will

PS: Set intent.flag_activity_new_task in the FLAG of intent:

1 New Intent (); 2 intent.setclass (secondactivity.  this, mainactivity. class ); 3 intent.setflags (intent.flag_activity_new_task); 4 startactivity (intent);

We note that in the above process, the serial number of the firstactivity is constant, but the secondactivity serial number is not unique, indicating that a new instance was not generated when jumping from secondactivity to firstactivity. However, a new instance is generated when jumping from firstactivity to secondactivity. The schematic diagram of the Singletask mode is as follows:

      

The lower half of the figure is the result of the change in the stack structure after secondactivity jumps to firstactivity, and we notice that the secondactivity disappears, yes, in this jump process the system discovers the existence of the firstactivity instance, Instead of generating new instances, the activity instances above the firstactivity are all stacked up, and the firstactivity becomes the top object of the stack and appears before the screen. Perhaps friends have doubts, if the secondactivity is also set to Singletask mode, then the secondactivity instance can not be unique? In our example is not possible, because each time from secondactivity jump to firstactivity, secondactivity instances are forced out of the stack, the next time firstactivity jump to Secondactivity, The existing secondactivity instance cannot be found, so a new instance must be generated. But if we have thirdactivity, let secondactivity and thirdactivity jump to each other, then the secondactivity instance can be guaranteed unique.

This is the Singletask mode, if a corresponding activity instance is found, the other activity instances above the activity instance are all out of the stack, so that the activity instance becomes the top of the stack object and appears before the screen.

4. SingleInstance

This startup mode is special because it enables a new stack structure, places the acitvity in the new stack structure, and guarantees that no other activity instances will enter.

We modified Firstactivity's launchmode= "standard", secondactivity launchmode= "SingleInstance", because of the multiple stack structure involved, We need to show the ID of the current stack structure in each activity, so we add the following code for each activity:

1 TextView taskidview = (TextView) Findviewbyid (R.id.taskidview); 2 this. GetTaskID ());

Then we'll show you the process:

      

We found that the two activity instances were placed in different stack structures, and the schematic diagram for singleinstance is as follows:

      

When we see the jump from firstactivity to secondactivity, we re-enable a new stack structure to place the secondactivity instance, then press the back key again to return to the original stack structure , the lower half of the figure is shown in secondactivity again to jump to firstactivity, this time the system will generate a firstactivity instance in the original stack structure, and then back two times, notice, did not exit, But to return to the secondactivity, why? Because when we jump from secondactivity to firstactivity, our starting point becomes the stack structure where the secondactivity instance resides, so we need to "go back" to this stack structure.

If we modify Firstactivity's Launchmode value to any of Singletop, Singletask, and singleinstance, the process will:

      

The SingleInstance startup mode may be the most complex mode, and to help you understand it, let me give an example, if we have a share application where the shareactivity is the entry activity and the activity that can be invoked by other applications. We set the activity's startup mode to SingleInstance and then call it in other apps. We edit the configuration of the shareactivity:

1 <ActivityAndroid:name=". Shareactivity "Android:launchmode= "SingleInstance">2     <Intent-filter>3         <ActionAndroid:name= "Android.intent.action.MAIN" />4         <categoryAndroid:name= "Android.intent.category.LAUNCHER" />5     </Intent-filter>6     <Intent-filter>7         <ActionAndroid:name= "Android.intent.action.SINGLE_INSTANCE_SHARE" />8         <categoryAndroid:name= "Android.intent.category.DEFAULT" />9     </Intent-filter>Ten </Activity>

Then we start the activity in other applications like this:

1 New Intent ("Android.intent.action.SINGLE_INSTANCE_SHARE"); 2 startactivity (intent);

When we open the shareactivity and then press the back button back to the original interface, shareactivity as a separate individual existence, if we open the share application, do not need to create a new shareactivity instance to see the results, because the system will automatically find , the existence is directly utilized. You can print the TaskID in shareactivity to see the effect. For this process, the schematic diagram is as follows:

      

Four kinds of launchmode for Android activity

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.