Activity of four kinds of launchmode.
Launchmode plays an important role in the process of multiple activity jumps, it can decide whether to generate new activity instances, whether to reuse existing activity instances, and whether to share a task with other activity instances. Here is a brief introduction to the concept of a task, which is an object with a stack structure, a task that can manage multiple activity, launch an application, and create a corresponding task.
The activity has the following four kinds of Launchmode:
1.standard
2.singleTop
3.singleTask
4.singleInstance
We can configure <activity> the Android:launchmode attribute in Androidmanifest.xml for one of the above four kinds.
Here we combine examples to introduce these four kinds of lanchmode:
1.standard
Standard mode is the default startup mode, you do not have to configure the Android:launchmode property for <activity>, and you can also specify a value of.
We will have an activity, named Firstactivity, to demonstrate the standard startup mode. The firstactivity code is as follows:
[Java]View Plaincopy
- Package Com.scott.launchmode;
- Import android.app.Activity;
- Import android.content.Intent;
- Import Android.os.Bundle;
- Import Android.view.View;
- Import Android.widget.Button;
- Import Android.widget.TextView;
- Public class Firstactivity extends Activity {
- @Override
- public void OnCreate (Bundle savedinstancestate) {
- super.oncreate (savedinstancestate);
- Setcontentview (R.layout.first);
- TextView TextView = (TextView) Findviewbyid (R.id.textview);
- Textview.settext (this.tostring ());
- Button button = (button) Findviewbyid (R.id.button);
- Button.setonclicklistener (new View.onclicklistener () {
- @Override
- public void OnClick (View v) {
- Intent Intent = new Intent (firstactivity. This, firstactivity. class);
- StartActivity (Intent);
- }
- });
- }
- }
The TextView in our Firstactivity interface is used to display the serial number of the current activity instance, and the button is used to jump to the next firstactivity interface.
Then we click the button a few times, there will be the following phenomenon:
We notice that the firstactivity are all examples, but the serial numbers are different, and we need to press the back key two times to go back to the first fristactivity. The principle of standard mode is as follows:
, each jump system generates a new Firstactivity instance in the task and is placed at the top of the stack structure, and when we press the back key, we can see the original firstactivity instance.
This is the standard startup mode, which generates a new instance, regardless of whether there are any existing instances.
2.singleTop
Based on the above, for <activity> specify the attribute android:launchmode= "Singletop", the system will follow the Singletop startup mode to handle the jump behavior. We repeat the above several actions, will appear the following phenomenon:
We see this result is different from standard, three serial numbers are the same, that is, using the same firstactivity instance; If you press the back key, the program exits immediately, indicating that there is only one activity instance in the current stack structure. The principle of Singletop mode is as follows:
As shown, the system will first look in the stack structure to see if there is a firstactivity instance on the top of the stack, and if there is no new one, use it directly. Perhaps friends will have doubts, I only see the stack only one activity, if it is a number of activity what to do, if not at the top of the stack? We will then pass an example to confirm the question.
Let's create a new activity named Secondactivity, as follows:
[Java]View Plaincopy
- Package Com.scott.launchmode;
- Import android.app.Activity;
- Import android.content.Intent;
- Import Android.os.Bundle;
- Import Android.view.View;
- Import Android.widget.Button;
- Import Android.widget.TextView;
- Public class Secondactivity extends Activity {
- @Override
- protected void OnCreate (Bundle savedinstancestate) {
- super.oncreate (savedinstancestate);
- Setcontentview (R.layout.second);
- TextView TextView = (TextView) Findviewbyid (R.id.textview);
- Textview.settext (this.tostring ());
- Button button = (button) Findviewbyid (R.id.button);
- Button.setonclicklistener (new View.onclicklistener () {
- @Override
- public void OnClick (View v) {
- Intent Intent = new Intent (secondactivity. This, firstactivity. class);
- StartActivity (Intent);
- }
- });
- }
- }
Then change the previous firstactivity jump code to:
[Java]View Plaincopy
- Intent Intent = new Intent (firstactivity. This, secondactivity. class);
- StartActivity (Intent);
Yes, Firstactivity will jump to secondactivity,secondactivity and will jump to firstactivity. The demo results are as follows:
We see that the serial numbers of the two firstactivity are different, proving that a new firstactivity instance was generated when jumping from secondactivity to firstactivity. Schematic diagram is as follows:
We see that when jumping from secondactivity to firstactivity, the system discovers that there are firstactivity instances, but not at the top of the stack, and then regenerates an instance.
This is the Singletop startup mode, and if a corresponding activity instance is found to be at the top of the stack, it is reused and no new instances are generated.
3.singleTask
Based on the above, we modify the properties of the firstactivity android:launchmode= "Singletask". The results of the demo are as follows:
We note that in the above process, the serial number of the firstactivity is constant, but the secondactivity serial number is not unique, indicating that a new instance was not generated when jumping from secondactivity to firstactivity. However, a new instance is generated when jumping from firstactivity to secondactivity. The schematic diagram of the Singletask mode is as follows:
The lower half of the figure is the result of the change in the stack structure after secondactivity jumps to firstactivity, and we notice that the secondactivity disappears, yes, in this jump process the system discovers the existence of the firstactivity instance, Instead of generating new instances, the activity instances above the firstactivity are all stacked up, and the firstactivity becomes the top object of the stack and appears before the screen. Perhaps friends have doubts, if the secondactivity is also set to Singletask mode, then the secondactivity instance can not be unique? In our example is not possible, because each time from secondactivity jump to firstactivity, secondactivity instances are forced out of the stack, the next time firstactivity jump to Secondactivity, The existing secondactivity instance cannot be found, so a new instance must be generated. But if we have thirdactivity, let secondactivity and thirdactivity jump to each other, then the secondactivity instance can be guaranteed unique.
This is the Singletask mode, if a corresponding activity instance is found, the other activity instances above the activity instance are all out of the stack, so that the activity instance becomes the top of the stack object and appears before the screen.
4.singleInstance
This startup mode is special because it enables a new stack structure, places the acitvity in the new stack structure, and guarantees that no other activity instances will enter.
We modified Firstactivity's launchmode= "standard", secondactivity launchmode= "SingleInstance", because of the multiple stack structure involved, We need to show the ID of the current stack structure in each activity, so we add the following code for each activity:
[Java]View Plaincopy
- TextView Taskidview = (TextView) Findviewbyid (R.id.taskidview);
- Taskidview.settext ("Current task ID:" + This.gettaskid ());
Then we'll show you the process:
We found that the two activity instances were placed in different stack structures, and the schematic diagram for singleinstance is as follows:
When we see the jump from firstactivity to secondactivity, we re-enable a new stack structure to place the secondactivity instance, then press the back key again to return to the original stack structure , the lower half of the figure is shown in secondactivity again to jump to firstactivity, this time the system will generate a firstactivity instance in the original stack structure, and then back two times, notice, did not exit, But to return to the secondactivity, why? Because when we jump from secondactivity to firstactivity, our starting point becomes the stack structure where the secondactivity instance resides, so we need to "go back" to this stack structure.
If we modify Firstactivity's Launchmode value to any of Singletop, Singletask, and singleinstance, the process will:
The SingleInstance startup mode may be the most complex mode, and to help you understand it, let me give an example, if we have a share application where the shareactivity is the entry activity and the activity that can be invoked by other applications. We set the activity's startup mode to SingleInstance and then call it in other apps. We edit the configuration of the shareactivity:
[HTML]View Plaincopy
- <activity android:name=". Shareactivity " android:launchmode="singleinstance ">
- <intent-filter>
- <action android:name="Android.intent.action.MAIN" />
- <category android:name="Android.intent.category.LAUNCHER" />
- </intent-filter>
- <intent-filter>
- <action android:name="Android.intent.action.SINGLE_INSTANCE_SHARE" />
- <category android:name="Android.intent.category.DEFAULT" />
- </intent-filter>
- </activity>
Then we start the activity in other applications like this:
[Java]View Plaincopy
- Intent Intent = new Intent ("Android.intent.action.SINGLE_INSTANCE_SHARE");
- StartActivity (Intent);
When we open the shareactivity and then press the back button back to the original interface, shareactivity as a separate individual existence, if we open the share application, do not need to create a new shareactivity instance to see the results, because the system will automatically find , the existence is directly utilized. You can print the TaskID in shareactivity to see the effect. For this process, the schematic diagram is as follows:
Android Four kinds of launchmode