Background:
In many activity development, it is possible that the activity jumps between applications, and may want to jump to the original activity instance instead of generating a large number of duplicate activity instances.
This requirement needs to be implemented by configuring the Launchmode of the activity.
There are four modes of loading for activity:
1.standard (Standard mode, this is the default load mode)
Activity in this mode will generate a new instance each time
We first defined the activity of a and then its contents are 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);//Gets the task stack for the current activity idint TaskID = A.this.gettaskid ();// Gets the details of the current activity instance, string objectmsg = A.this.tostring (),//outputs the information to the screen Textview01.settext ("Current task Stack ID:" + taskID + "\ n" + " Current Activity Instance information: "+ objectmsg);} @Overridepublic void OnClick (View v) {switch (V.getid ()) {r.id.button1://starts jump activityintent intent = new Intent (a.th is, a.class); startactivity (intent); break;default:break;}}}
after the program is run, we click "Jump to a" button 2 times, you will find three different interfaces:
Observed that: The three A is a task stack, because the stack ID are the same, but their instance name is different, indicating that the task stack now has three distinct a instances, if you click on the phone on the "Back" button, then you need to click three consecutive times to quit the program.
Summarize the above into a flowchart as follows:
2.singleTop mode
In this mode,
If a is at the top of the task stack, then a jumps to a will not generate a new instance, jump directly to the current stack top A;
If a is not at the top of the task stack, then the other activity jumps to a and a new instance of a is generated;
We define two activity in code, A and B, which are the definitions of these two activity, where A's Launchmode property 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);// Gets the current activity of the task stack idint TaskID = A.this.gettaskid ();//Gets the details of the current activity instance string objectmsg = A.this.tostring ();// Output the information to the screen 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 (A.thi S, A.CLass); startactivity (intent01); break;case r.id.button2://a---->bintent intent02 = new Intent (a.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);// Gets the current activity of the task stack idint TaskID = B.this.gettaskid ();//Gets the details of the current activity instance string objectmsg = B.this.tostring ();// Output the information to the screen 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.thi S, A.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 runs, we click to jump to a, the interface will not have any changes, when a on the top of the task stack, you send a jump to a intent, then this intent will be sent to the top of a instance;
The following three pictures show the flow of a---->b---->a:
Observation: These three activity instances are different instances, but they belong to a task stack, because their task stack ID is the same; we can obviously find that when B is at the top of the stack, jumping to a will generate a new instance of a.
Summarize the above into a flowchart as follows:
3.singleTask mode
In this mode, there can be only one instance of a in the task stack, regardless of where it is in the task stack;
Here we should note that when a is at the top of the stack, it jumps directly to the a instance, and when a is not at the top of the stack, what happens is that all activity instances above A are ejected from the task stack until a reaches the top of the stack.
This time the last code is still in use, just in manifest A's launchmode is configured as Singletask, a button in B jumps to C, and then there is only one button in C, the function of this button is to jump to a.
The following four images show a---->b---->c---->a:
Observe the above results we can find: a total of three instances, a, B, c each, when C at the top of the stack, jump to a, we will return, intent will be sent directly to the bottom of a instance, and the B, c two instances of the stack.
Summarize the above into a flowchart as follows:
4.singleInstance mode
This activity loading mode is designed to solve such a problem:
In the same app, when an activity needs to be shared by multiple activity (that is, multiple activity can jump to the activity), then we certainly want this activity to be in a alive state for a long time, And it's the same instance (which saves cell phone resources), but in the same app, all the activity instances are in the same task stack, and we find that regular stacks and stacks make it impossible to determine when the activity will be destroyed by the stack. At this point we can satisfy this requirement by setting the activity's load mode to SingleInstance, when set to this mode, the activity is maintained separately in a task stack, not affected by the frequent stack and reload operations in the main task stack.
Actually write to this place I think the expression of the text is always less than the graphic, first we look at, the B launchmode set to SingleInstance, and then to analyze the following picture results:
Analysis of the above results we will find: b instance of the task stack ID, and a instance of the task stack ID is not the same, indicating that they are in a different task stack;
And this is just a situation, actually there are several cases, I summarize it into the following picture, hoping to be able to help the reader really;
Write this article when I think of the ancients of a word, the paper came to the end of shallow, I know this matter to preach , online popular a talk about Launchmode articles, all with blue illustrations, in fact, I have benefited from this article, but when I wrote this article, I found " I think the former I fully understand "is not comprehensive, I seem to have a new understanding of lauchmode, thanks to practice!!!
Here is the last one I painted Flowchart:
Routine, put the code link here:
Android Activity Launchmode
Android Activity Launchmode