Detailed description of activity Startup Mode in Android

Source: Internet
Author: User

In Android, each interface is an activity. Switching an interface operation is actually an instantiation operation between multiple different activities. In Android, the start mode of the activity determines the start mode of the activity.

There are four startup modes for the total Android activity:

Activity startup mode setting: <activity Android: Name = ". mainactivity "Android: launchmode =" standard "/> four startup modes of activity: 1. in standard mode, an activity is created every time an activity is activated and placed in the task stack. 2. singletop: if an instance of the activity exists at the top of the stack of the task, the instance will be reused, otherwise, a new instance will be created and placed on the top of the stack (even if the activity instance already exists in the stack, the instance will be created as long as it is not on the top of the stack ). 3. If a singletask has an instance of the activity in the stack, it will reuse the instance (onnewintent () of the instance will be called ()). When it is reused, the instance will be sent back to the top of the stack, so the instance on it will be removed from the stack. If this instance does not exist in the stack, a new instance is created and put into the stack. 4. singleinstance creates the activity instance in a new stack and allows multiple applications to share and modify the activity instance in the stack. Once an instance of a modified activity exists in a stack, any application that activates or changes the activity will reuse the instance in the stack. The effect is that multiple applications share one application, no matter who activates the activity, it will enter the same application.

 

Standard is the default startup mode.

 

The following example demonstrates the Standard Operating Mechanism:

1 private textview text_show; 2 Private button btn_mode; 3 4 @ override 5 Public void oncreate (bundle savedinstancestate) {6 super. oncreate (savedinstancestate); 7 setcontentview (R. layout. activity_main); 8 9 text_show = (textview) This. findviewbyid (R. id. text_show); 10 11 text_show.settext (this. tostring (); 12 13 btn_mode = (button) This. findviewbyid (R. id. btn_mode); 14 15} 16
// Click Event 17 Public void launchstandard (view v) {18 startactivity (new intent (this, mainactivity. class); 19 20 text_show.settext (this. tostring (); 21}

The initialization interface is as follows:

  

When you click the button, a new activity will be created. You can see through the display of the hexadecimal number after textview @. The two tabs are displayed as follows:

  

  

At this point, we analyze the internal operation mechanism of the stack:

(From top to top)

 

Therefore, this standard mode creates a new activity object every time. When you click the return button, it eliminates the top stack (current activity) and jumps to the next layer, for example, if the current activity is 44ed8c50, then the activity will change to 44f28a48 when we click return. However, when we click Create object again in this activity, it will create another activity object, this mode may not be required in most cases because it consumes too much system performance.

The following describes two startup modes that can use the activity in the current stack:

2. singletop

From the above explanation, we can know that every time a new activity is used, it will automatically detect whether the current activity at the top of the stack is the activity to be referenced. If yes, it will be referenced directly, instead of creating a new activity.

We added a "Start singletop mode" button to the interface just now. When we click the button, the singletop we created appears. There is a button in activity singletop to start singletop mode, indicates to start the current activity. Because we configure the activity startup mode as singletop in the list file, we will not create any more but use the singletop activity at the top of the current stack:

     <activity            android:name=".SingleTopActivity"            android:label="@string/singletop"            android:launchMode="singleTop" >        </activity>

 

 

Interface initialization:

      

Click "Start singletop mode:

We can analyze its running mechanism and we can see that when the program runs, the data format in the stack is:

    

When we click the "Start singletop mode" button on the Interface above, because this activity sets the start mode to singletop, therefore, it first checks whether the current top of the stack is the activity object we want to request. After verification, it will not create a new activity, but reference the activity at the top of the current stack.

Although it does not create a new activity object, it calls the onnewintent () method every time:

1 @Override2     protected void onNewIntent(Intent intent) {3         // TODO Auto-generated method stub4         super.onNewIntent(intent);5         6         Toast.makeText(this, new Date().toString(), 1).show();7     }

 

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

 

3. singletask

The difference between this startup mode and singletop is that singletop only checks whether the activity at the top of the current stack needs to be created, singletask detects all activity objects in the stack. From top to bottom, if we detect that the activity object is requested, the objects above the activity object will be eliminated, directly set the detected activity to the top of the stack.

We create a singletaskactivity, which contains a button to start mainactivity and to start singletaskactivity.

Initialization:

    

Click "Start singletask mode:

    

Click the second button "Start singletask mode" in this interface to check whether the activity object exists in the current stack according to the definition. Therefore, the current activity is displayed and will not be created again;

Click "Start standard mode". Because the mainactivity startup mode is standard, a new mainactivity object will be created here:

    

The data format in the stack is:

    

When you click the "Start singletask mode" button on the Interface above, because the second activity we want to create in the current stack is detected, the top mainactivity is eliminated, set singletaskactivity to the top of the stack:

    

 

4. singleinstance

This startup mode works similar to the browser we use. We all know that when accessing a browser in multiple programs, if the current browser does not open, open the browser, otherwise, it will be accessed in the current browser. This mode saves a lot of system resources because it ensures that only one activity object to be requested exists in the current stack.

 

    

The above is the four startup modes in Android, which we often use when developing Android projects. cleverly setting the Startup Mode of activity will save system overhead and program running efficiency.

  

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.