Four kinds of launchmode_android of activity in Android development

Source: Internet
Author: User
Tags home screen

The activity stack is primarily used to manage the shift of activity. When using intent to jump to a target activity, it needs to be loaded according to the load mode of the target activity.

The activity altogether has the following four kinds of Launchmode:

1.standard: By default, a new instance is created each time you use intent to jump to the target activity. The downside is that every time you enter, you create a new instance and execute the OnCreate method.

2.singleTop: If the target activity to jump is right at the top of the task (stating that it is not currently in the target task, for example, I am on the micro-credit page, and then I want to use intent to jump to the INNOXYZ application's homepage, So join the INNOXYZ home page just at the top of the INNOXYZ task, jump directly and not create an instance, then jump directly to the past without creating a new. (for example, currently in Home screen, you receive a tweet on the front page, and at this point in the task stack, the top of the activity stack in the micro-task is just the front page, so click Push to go directly to the instance without creating a new instance).

3.singleTask: this instance will be generated in a new task, which will be used every time you call it, and will not produce a new instance.

4.singleInstance: Is the only activity on its stack, which is reused every time.

Launchmode plays an important role in the process of multiple activity jumps, and it can decide whether to generate new activity instances, reuse existing activity instances, and share a task with other activity instances. Here is a brief introduction to the concept of task, a task is an object with a stack structure, a task can manage multiple activity, start an application, and create a task corresponding to it.

We can androidmanifest.xml configure <activity> Android:launchmode properties for one or more of the above four.
Here we combine the examples to introduce these four kinds of lanchmode:
1.standard
The standard mode is the default boot mode, but you can also specify a value of standard, without having to configure the Android:launchmode property for <activity>.
We will have an activity, named Firstactivity, to demonstrate the standard boot mode. The firstactivity code is as follows:

The standard mode is the default boot mode, but you can also specify a value of standard, without having to configure the Android:launchmode property for <activity>.
We will have an activity, named Firstactivity, to demonstrate the standard boot mode. The firstactivity code is as follows:

Package Com.scott.launchmode; 
 
Import android.app.Activity; 
Import android.content.Intent; 
Import Android.os.Bundle; 
Import Android.view.View; 
Import Android.widget.Button; 
Import Android.widget.TextView; 
 
public class Firstactivity extends activity { 
  @Override public 
  void OnCreate (Bundle savedinstancestate) { 
    Super.oncreate (savedinstancestate); 
    Setcontentview (R.layout.first); 
    TextView TextView = (TextView) Findviewbyid (R.id.textview); 
    Textview.settext (This.tostring ()); 
    Button button = (button) Findviewbyid (R.id.button); 
    Button.setonclicklistener (New View.onclicklistener () { 
      @Override public 
      void OnClick (View v) { 
        Intent Intent = new Intent (firstactivity.this, firstactivity.class); 
        StartActivity (intent);}} 
    ); 
  } 
 

The TextView in our Firstactivity interface is used to display the serial number of the current activity instance, and the button is used to jump to the next firstactivity interface.
Then we click the button several times in a row and the following phenomena will appear:

We notice that the firstactivity are all instances, but the serial numbers are different, and we need to press the back key two times in succession to get back to the first fristactivity. The principle of standard mode is shown in the following illustration:

As shown in the diagram, each jump system generates a new Firstactivity instance in the task, and it is placed at the top of the stack structure to see the original firstactivity instance when we press the back key.
This is the standard startup mode, which generates a new instance, regardless of any existing instances.

2.singleTop
on the basis of the above, we specify the attribute android:launchmode= "Singletop" for <activity>, and the system will process the jump behavior according to the Singletop startup mode. We repeat the above actions, the following phenomena will appear:

We see this result is different from standard, three serial numbers are the same, that is to use the same firstactivity instance; If you click 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 shown in the following illustration:

As shown in the figure above, 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 longer generate new, it is used directly. Perhaps friends will have questions, I only see only one activity on the stack, if it is multiple activity how to do, if not at the top of the stack how? We'll go through an example to confirm the question.
We'll create a new activity named Secondactivity, as follows:

Package Com.scott.launchmode; 
 
Import android.app.Activity; 
Import android.content.Intent; 
Import Android.os.Bundle; 
Import Android.view.View; 
Import Android.widget.Button; 
Import Android.widget.TextView; 
 
public class Secondactivity extends activity { 
  @Override 
  protected void onCreate (Bundle savedinstancestate) { C10/>super.oncreate (savedinstancestate); 
    Setcontentview (R.layout.second); 
    TextView TextView = (TextView) Findviewbyid (R.id.textview); 
    Textview.settext (This.tostring ()); 
    Button button = (button) Findviewbyid (R.id.button); 
    Button.setonclicklistener (New View.onclicklistener () { 
      @Override public 
      void OnClick (View v) { 
        Intent Intent = new Intent (secondactivity.this, firstactivity.class); 
        StartActivity (intent);}} 
    ); 
  } 
 

Then change the previous firstactivity jump code to:

Intent Intent = new Intent (firstactivity.this, Secondactivity.class); 


Yes, firstactivity jumps to secondactivity,secondactivity and jumps to firstactivity. The results of the demo are 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. The schematic diagram is as follows:

We see that when we jump 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, reuse it and no longer generate a new instance.

3.singleTask
on the basis of the above we modify the properties of Firstactivity android:launchmode= "Singletask". The results of the demo are as follows:

We notice that in the process above, the serial number of the firstactivity is invariant and the secondactivity serial number is not unique, which shows that when you jump from secondactivity to firstactivity, no new instances are generated. However, a new instance is generated when you jump from firstactivity to secondactivity. The schematic diagram of the singletask pattern is shown in the following illustration:

In the lower part of the diagram is the secondactivity jump to the firstactivity after the stack structure changes results, we notice that secondactivity disappeared, yes, in this jump process, the system found that there is a firstactivity instance, Instead of generating a new instance, the activity instance on top of the firstactivity is put out of the stack, and the firstactivity becomes the top object of the stack, which is displayed in front of the screen. Perhaps friends have doubts, if the secondactivity also set to Singletask mode, then secondactivity instance can be unique? This is not possible in our example, because every time you jump from secondactivity to firstactivity, secondactivity instances are forced out of the stack, and the next time firstactivity jumps 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 each other, then the Secondactivity instance can guarantee the unique.
This is the Singletask mode, and if a corresponding activity instance is found, the other activity instances on top of this activity instance are all stack, making the activity instance the top of the stack, and appearing before the curtain.

4.singleInstance
This startup mode is special because it enables a new stack structure, places the acitvity in the new stack structure, and ensures that no other activity instances are entered.
We modify Firstactivity's launchmode= "standard", secondactivity launchmode= "SingleInstance", because of the multiple stack structure involved, We need to display the ID of the current stack structure in each activity, so we add the following code for each activity:

TextView Taskidview = (TextView) Findviewbyid (R.id.taskidview); 
Taskidview.settext ("Current task ID:" + this.gettaskid ()); 

And then we'll show you this process:

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

We see that when we jump from firstactivity to secondactivity, we re-enable a new stack structure to place the secondactivity instance and then press the back key to return to the original stack structure again. The lower half of the figure is shown in secondactivity to jump to firstactivity again, when the system generates a firstactivity instance in the original stack structure and then returns two times, noting, and not exiting, But back to the secondactivity, why? Because when we jump from secondactivity to firstactivity, our starting point becomes the stack structure where the secondactivity instance is located, so we need to "return" to the stack structure.
If we modify any of the firstactivity Launchmode values as Singletop, Singletask, and singleinstance, the process will look like the following:

SingleInstance startup mode is probably one of the most complex patterns, and in order to help you understand it, let me give you an example, if we have a share application where the shareactivity is a portal activity and an activity that can be invoked by other applications, We set the activation mode of this activity to singleinstance and then call it in other applications. We edit the shareactivity configuration:

<activity android:name= ". Shareactivity "android:launchmode=" singleinstance "> 
  <intent-filter> 
    <action android:name=" Android.intent.action.MAIN "/> 
    <category android:name= android.intent.category.LAUNCHER"/> 
  < /intent-filter> 
  <intent-filter> 
    <action android:name= "Android.intent.action.SINGLE_INSTANCE _share "/> 
    <category android:name= android.intent.category.DEFAULT"/> 
  </intent-filter> 


We then start the activity in other applications:

Intent Intent = new Intent ("Android.intent.action.SINGLE_INSTANCE_SHARE"); 
StartActivity (Intent); 

When we open the shareactivity and then press the back button back to the original interface, shareactivity as a separate individual exists, 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 used directly. We can print the TaskID in the shareactivity to see the effect. The schematic diagram of this process is as follows:

Related Article

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.