Four startup modes of Android activity

Source: Internet
Author: User

Four startup modes of Android activity

Four startup modes in Activity

In AndroidManifest. in xml, there is a default activity in which the activity startup mode can be set. android: launchMode = "", this attribute is used to configure the loading mode of the Activity, this attribute supports different effects for each mode of the attribute in Step 4. The following describes the startup mode.

Standard: standard mode, default Loading Mode

SingleTop: Task top Singleton Mode
SingleTask: singleTask Singleton Mode
SingleInstance: Global singleInstance Mode


1 Why do Activity use the specified mode ??

First, we will introduce Android Activity management: Android uses tasks to manage multiple activities. When we start an Activity, the system will create a Task and then start the portal of the Activity.
Android does not provide APIs for tasks. You can only call the getTaskId () method of an Activity to obtain the ID of the Task where the Task is located. we can regard the Task as an Activity stack, and the Task manages the Activity by stack.


2. Four startup Modes


Standard Loading Mode:

Every time an Activity is started in this mode, Android always creates a new instance for the Activity to be started and adds the Activity to the current Task stack. In this mode, no new Task is created, just change the new
Add Activity to the original Task


SingleTop Mode


If an instance of the Activity exists at the top of the stack of the task, the instance is reused. Otherwise, a new instance is created and placed on the top of the stack (even if the Activity instance already exists in the stack, instances will be created as long as they are not at the top of the stack ).


SingleTask Mode


The started Activity has only one Activity instance in the same Task, which can be divided into the following three situations:


<1>. If the started target Activity does not exist in the Task stack, the system creates a target Activity instance and adds it to the top of the Task stack.


<2>. If the target Activity to be started already has a Task stack top, the mode is the same as the singleTop mode.


<3>. If the target Activity to be started already exists but is not at the top of the Task stack, the system will remove all the activities on the target Activity from the Task stack so that the Activity is placed at the top of the Task stack.


SingleInstance Mode

In this loading mode, no matter which Task starts the target Activity, only one target Activity instance is created and a new Task stack is used to load the Activity instance. There are two situations:


<1>. If the created target Activity does not exist, the system will first create a new Task, create an Activity instance, and then add the target Activity to the top of the new Task stack.


<2> If the created target Activity already exists, the system places the stack of the Activity in the foreground no matter which Task stack it belongs.


Note: The Activity in singleInstance loading mode is always at the top of the Task stack, and the Task stack of the Activity only contains the Activity.



3. Four startup Modes


Standard

Standard is the default startup mode.

The following example demonstrates the standard Operating Mechanism:

Private Button btn_mode; @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); text_show = (TextView) this. findViewById (R. id. text_show); text_show.setText (this. toString (); btn_mode = (Button) this. findViewById (R. id. btn_mode);} // click the event public void LaunchStandard (View v) {startActivity (new Intent (this, MainActivity. class); text_show.setText (this. toString ());

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 of the stack in sequence)

Therefore, this Standard mode creates a new Activity object every time. When the return button is clicked, 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.


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:

 


The initialization interface is as follows:

When you 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:

@Override      protected void onNewIntent(Intent intent) {         // TODO Auto-generated method stub         super.onNewIntent(intent);         Toast.makeText(this, new Date().toString(), 1).show();     }

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.


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:

When you click the "Start singletop mode" button:


In this interface, click the second button "Start singleTask mode". According to the definition, the system checks whether the Activity object exists in the current stack. Therefore, the current Activity is displayed and no re-creation is performed;


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


In this case, the ordered 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:

The working principle of the browser is similar. We all know that when you access the browser in multiple programs, if the current browser does not open, open the browser; otherwise, access will be made 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.