Four startup modes in activity

Source: Internet
Author: User

Every interface in Android is an activity, and switching interface operations is actually an instantiation of multiple different activity. The startup mode of activity in Android determines how the activity starts running.

The startup mode of Android total activity is divided into four types:

[HTML]View Plaincopy
  1. Activity startup mode settings:
  2. <activity android:name=". Mainactivity " android:launchmode=" standard" />
  3. Four startup modes of activity:
  4. . Standard
  5. mode startup mode, activity is created each time the activity is activated and placed in the task stack.
  6. . Singletop
  7. 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).
  8. . Singletask
  9. If an instance of the activity is already in the stack, the instance is reused (Onnewintent () of the instance is invoked). When reused, the instance is returned to the top of the stack, so the instances above it will be removed from the stack. If the instance does not exist in the stack, a new instance is created to be put into the stack.
  10. . SingleInstance
  11. Create the activity instance in a new stack and have multiple apps share the activity instance in the stack. Once an instance of the activity in the schema is present on a stack, any application that activates the activity will reuse the instance in that stack, with the effect of sharing an app with multiple applications, regardless of who activates the activity into the same application.


Where standard is the default startup mode for the system.

The following example illustrates the operating mechanism of standard:

[Java]View Plaincopy
  1. Private TextView text_show;
  2. private Button Btn_mode;
  3. @Override
  4. public void OnCreate (Bundle savedinstancestate) {
  5. super.oncreate (savedinstancestate);
  6. Setcontentview (R.layout.activity_main);
  7. Text_show = (TextView) This.findviewbyid (r.id.text_show);
  8. Text_show.settext (this.tostring ());
  9. Btn_mode = (Button) This.findviewbyid (R.id.btn_mode);
  10. }
  11.     button click event
  12. public void Launchstandard (View v) {
  13. StartActivity (new Intent (this,mainactivity.   Class));
  14. Text_show.settext (this.tostring ());
  15. }


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.

Here we describe two boot modes that can use the activity in the current stack:

2. 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:

[HTML]View Plaincopy
    1. <activity
    2. android:name=". Singletopactivity "
    3. android:label="@string/singletop"
    4. android:launchmode="Singletop" >
    5. </activity>

Interface initialization:

      

Click on 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:

[Java]View Plaincopy
    1. @Override   
    2.      protected void onnewintent (intent intent)  {  
    3.           // todo auto-generated method stub  
    4.          super.onnewintent ( Intent);   
    5.             
    6.          toast.maketext (this ,  new date (). toString (),  1). Show ();  
    7. &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;}&NBSP;&NBSP;


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.

3.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

    

Click on the "Start Singletask 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 data format in this stack is:

    

When you click on the "Start Singletask Mode" button in the upper interface, we will destroy the topmost mainactivity and then set the singletaskactivity to the top of the stack as we detect that the second activity in the current stack is the one we want to create:

    

4.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 running efficiency by cleverly setting activity startup mode.

Four startup modes in 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.