Android------four modes of loading

Source: Internet
Author: User

Four loading modes for Android:

First, when you configure activity, you specify which load mode to use for the activity, through the Android:launchmode property.

Four modes of loading:

Standard: Normal mode, default

Singletop:task Top Single case mode

Singletask:task in single-case mode

SingleInstance: Global Singleton mode

Android manages activity through a task, and the ID of the currently active task can be obtained through the GetTaskID () method.

The task can be understood as the activity stack, and the task will manage the activity in the form of a stack.

Here are four modes:

1.standard mode

Each time the target activity is started by this mode, Android creates a new instance of the target activity and adds the dry activity to the current task stack----This mode does not start a new task. The new activity will be added to the original task.


Import Android.app.activity;import android.content.intent;import Android.os.bundle;import Android.view.Menu;import Android.view.menuitem;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.button;import Android.widget.linearlayout;import Android.widget.textview;public class MainActivity Extends Activity implements Onclicklistener {@Overrideprotected void onCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); LinearLayout layout = new LinearLayout (mainactivity.this); layout.setorientation (1); Setcontentview (layout); TextView TextView = new TextView (mainactivity.this); Textview.settext ("Activity is:" + this.tostring () + "\ n" + ", the Task ID is: "+ This.gettaskid ()); Button button = New button (this), Button.settext ("Start main"), Layout.addview (TextView); Layout.addview (button); Button.setonclicklistener (this);} @Overridepublic void OnClick (View v) {//TODO auto-generated method Stubintent Intent = new Intent (Mainactivity.this, Main Activity.class); startactiVity (intent);}} 





Multiple click button, the program will continue to start the current activity, the activity of the hashcode changes, but TaskID always the same, which indicates that this loading mode does not start a new task.

When you press the back key, the activated activity is deleted from the top of the stack.




2.singleTop

This pattern is basically similar to the standard pattern, but it is a bit different: when the target activity that is going to be started is already at the top of the stack, the system does not recreate the target activity instance, but instead directly reuse the existing activity instance.

Change the Launchmode in the example above to singletop, no matter how many times the user clicks the button, the program on the interface will not change.

If the target activity that is to be started is not at the top of the task stack, the target activity instance is recreated and loaded into the top of the task's stack----at this time identical to the standard mode.



3.singleTask


Activity with this loading mode has only one instance within the same task, and when the target activity is enabled, there are three scenarios:

1. If the activity you want to start does not exist, create it and put it on top of the stack

2. If the target activity already exists at the top of the stack, the same as Singletop

3. If the target activity already exists, but is not at the top of the stack, the system will move the activity before the target activities out of the task stack so that the target is at the top

Description of the third case:

There are two activities in the program, one TextView and one button,textview used to display the active hashcode and Taskid,button to initiate different activities.

The second activity is started when the button in the first activity is pressed, and the first activity is started when the button in the second activity is pressed.








1 indicates activity 1 and its load mode

2 indicates activity 2 and its load mode

This is what runs with.

First create and start the 1, then create the start 2, then create the start 1, and then start 2, already 2, will not be created, so the 2 before the 1 moved out of the stack, so that 2 is at the top of the stack.

If you then start 1, this 1 will be created again, is not the original activity 1.

The third case above should be fully explained.



4.singleInstance



In this mode of loading, the system guarantees that the target activity will be started regardless of which task it is from, only one target activity instance is created and a new task stack is used to mount the active instance

When the system uses this mode to start the target activity, there are two situations:

1. If the activity that you want to start does not exist, create a task first, create the target activity, and add the stack top

2. If the target activity already exists, regardless of which application it is in, the system will move the task stack where the activity is located to the foreground to use that activity to display

Loading the activity in singleinstance load mode is always at the top of the task stack because there is only one activity in the task stack.

Change the load mode of the two activities in the previous example to SingleInstance.





Android------four modes of loading

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.