Android Activity LaunchMode and androidlaunchmode

Source: Internet
Author: User

Android Activity LaunchMode and androidlaunchmode

Background:

In many Activity development projects, Activity jumps between applications of the current user may wish to jump to an original Activity instance, rather than generating a large number of duplicate Activity instances.

You need to configure the LaunchMode of the Activity.


Activity has four loading modes:

1. standard (standard mode, which is the default Loading Mode)

In this mode, the Activity generates a new instance every time.


We first define the Activity A, and its content is as follows:

Import android. app. activity; import android. content. intent; import android. OS. bundle; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. textView; public class A extends Activity implements OnClickListener {private TextView textView01; private Button btn01; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_a); textView01 = (TextView) findViewById (R. id. TV _01); btn01 = (Button) findViewById (R. id. button1); btn01.setOnClickListener (this); // obtain the task stack IDint taskID of the current Activity =. this. getTaskId (); // obtain the details of the current Activity instance String objectMsg =. this. toString (); // output the information to textView01.setText ("current task stack ID:" + taskID + "\ r \ n" + "Current Activity instance information: "+ objectMsg) ;}@ Overridepublic void onClick (View v) {switch (v. getId () {case R. id. button1: // start to jump to ActivityIntent intent = new Intent (. this,. class); startActivity (intent); break; default: break ;}}}


After the program runs, we click the jump to A button twice to find three different interfaces:



It is observed that these three A instances belong to the same task stack because the stack IDs are the same, but their instance names are different, three different A instances exist in the task stack. If you click "return" on your mobile phone, you need to click "return" three times in A row to exit the program.


The preceding flowchart is summarized as follows:



2. singleTop Mode

In this mode,

If A is at the top of the job stack, A jump to A will not generate A new instance, directly jump to the current stack top;

If A is not at the top of the task stack, other activities will jump to A, and A new A instance will be generated;


We define two activities in the Code, namely A and B. The preceding two activities are defined. The launchMode attribute of A is set to singleTop.

Import android. app. activity; import android. content. intent; import android. OS. bundle; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. textView; public class A extends Activity implements OnClickListener {private TextView textView01; private Button btn01; private Button btn02; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_a); textView01 = (TextView) findViewById (R. id. TV _01); btn01 = (Button) findViewById (R. id. button1); btn01.setOnClickListener (this); btn02 = (Button) findViewById (R. id. button2); btn02.setOnClickListener (this); // obtain the task stack IDint taskID of the current Activity =. this. getTaskId (); // obtain the details of the current Activity instance String objectMsg =. this. toString (); // output the information to textView01.setText ("current task stack ID:" + taskID + "\ r \ n" + "Current Activity instance information: "+ objectMsg) ;}@ Overridepublic void onClick (View v) {switch (v. getId () {case R. id. button1: // A ---> AIntent intent01 = new Intent (. this,. class); startActivity (intent01); break; case R. id. button2: // A ----> BIntent intent02 = new Intent (. this, B. class); startActivity (intent02); break; default: break ;}}}


Import android. app. activity; import android. content. intent; import android. OS. bundle; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. textView; public class B extends Activity implements OnClickListener {private TextView textView01; private Button btn01; private Button btn02; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_a); textView01 = (TextView) findViewById (R. id. TV _01); btn01 = (Button) findViewById (R. id. button1); btn01.setOnClickListener (this); btn02 = (Button) findViewById (R. id. button2); btn02.setOnClickListener (this); // obtain the task stack IDint taskID of the current Activity = B. this. getTaskId (); // obtain the details of the current Activity instance String objectMsg = B. this. toString (); // output the information to textView01.setText ("current task stack ID:" + taskID + "\ r \ n" + "Current Activity instance information: "+ objectMsg) ;}@ Overridepublic void onClick (View v) {switch (v. getId () {case R. id. button1: // B ---> AIntent intent01 = new Intent (B. this,. class); startActivity (intent01); break; case R. id. button2: // B ----> BIntent intent02 = new Intent (B. this, B. class); startActivity (intent02); break; default: break ;}}}


After the program is running, we click to jump to A. The interface will not change. This means that when A is at the top of the task stack, you will send the Intent to jump to, this Intent will be sent to the instance at the top of the stack;

The process for displaying the following three images is A ----> B ---->:



We can see that these three Activity instances are different, but they belong to the same task stack because their task stack IDs are the same, when B is at the top of the stack, it jumps to A and A new A instance is generated.


The preceding flowchart is summarized as follows:



3. singleTask Mode

In this mode, the task stack can only have one A instance, no matter where it is located;

Note: When A is at the top of the stack, it directly jumps to instance A. When A is not at the top of the stack, what will happen, the task stack pops up for all Activity instances on A until A reaches the top of the stack.


The previous code is still used this time. In manifest, configure the launchMode of a as singleTask, add A Button in B to jump to C, and then there is only one Button in C, this button is used to jump to.

The following four pictures show A ----> B ----> C ---->:

 


Looking at the above results, we can find that there are three instances in total, each of which is A, B, and C. When C is at the top of the stack, it jumps to A, and we will return, intent will be directly sent to the instance at the bottom of the stack, and the B and C instances will play the stack.


The preceding flowchart is summarized as follows:



4. singleInstance Mode

The Activity loading mode is used to solve the following problems:

In the same App, when one Activity needs to be shared by multiple activities (that is, multiple activities can jump to this Activity), we certainly hope that this Activity remains Alive for a long time, and it is the same instance (which can save cell phone resources more), but in the same app, all Activity instances are in the same job stack, we will find that, the frequent pressure on stacks and bullet stacks make us unable to determine when the Activity will be destroyed by the bullet stack, at this time, we can set the loading mode of this Activity to singleInstance to meet this requirement. After this mode is set, this Activity is maintained separately in a task stack, it is not affected by frequent stack pressure and elastic stack operations in the master Task Stack.


In fact, I think the text is not expressive as a graph. First, let's take a look at setting the launchmode of B to singleInstance, and then analyze the following image results:


After analyzing the above results, we will find that the task stack ID of instance B is different from the task stack ID of instance A, which means they are in different task stacks;

In addition, this is only one case. In fact, there are several situations. I will summarize them in the figure below, hoping to help readers;


When I wrote this article, I remembered a sentence from the Ancients,On paper, I have to be enlightened.An article about LaunchMode is popular on the Internet, all of which are illustrated in blue. In fact, I have benefited from this article, however, when I wrote this article, I found that "I think I fully understand the past" is not comprehensive. I seem to have a new understanding of LauchMode. Thank you for your practice !!!


Here is the flowchart of the last one I painted:




For routine purposes, put the code link here:

Android Activity LaunchMode

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.