"Turn" Android Summary series: Activity startup mode (Lauchmode)

Source: Internet
Author: User

Transferred from: http://www.cnblogs.com/lwbqqyumidi/p/3771542.html

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:

 PackageCom.scott.launchmode;Importandroid.app.Activity;Importandroid.content.Intent;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.widget.Button;ImportAndroid.widget.TextView; Public classFirstactivityextendsActivity {@Override Public voidonCreate (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 (NewView.onclicklistener () {@Override Public voidOnClick (View v) {Intent Intent=NewIntent (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.

Simple understanding: Standard startup mode activity stack from the bottom of the stack to the top of the stack is A1, B, C, A2 .... (where a, B, C, etc. all represent different instances of activity, A1 and A2 are different instances of the same activity Class)

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:

 PackageCom.scott.launchmode;Importandroid.app.Activity;Importandroid.content.Intent;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.widget.Button;ImportAndroid.widget.TextView; Public classSecondactivityextendsActivity {@Overrideprotected voidonCreate (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 (NewView.onclicklistener () {@Override Public voidOnClick (View v) {Intent Intent=NewIntent (secondactivity. This, Firstactivity.class);                            StartActivity (Intent);    }        }); }}

Then change the previous firstactivity jump code to:

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.

Simply understood, Singletop is the current activity stack in the "Top of the stack", Activity Jump order or Standard mode if the stack structure is : A, B, C, D1, and D2, then The Singletop startup mode is:A, B, C, D1( at this time callback D1 onnewintent ().).

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 the activity stack is found to have a corresponding activity instance, so that the activity instance above the other activity instances out of the stack, so that the activity instance becomes the top of the stack object, display before the screen.

Simple understanding, Singletask represents the current activity stack "instance unique", activity Jump order or Standard mode of the stack structure if : A, B1, C, D, and B2, then Singletask startup mode is: A-B1 ( callback Onnewintent ()...)

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:

TextView Taskidview = (TextView) Findviewbyid (R.id.taskidview); Taskidview.settext (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.

The explanation here is not very agreed, the first time you press the back key is the stack top element in the current activity stack, and then show the current activity stack in the next activity stack, this is not explained, and then press the back key, not to go to the phone desktop, Instead of going back to the secondactivityinstance in another activity stack, I think the reason is that the "recent stack", as long as the stack is in the last home operation, will show it first.

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:

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

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:

The original author's explanation here may be a bit misleading. when we open the shareactivity and then press the back button back to the original interface, shareactivity as a separate individual existence, here should not be the press of the return key, but the home key, because once you press the return key, Shrareactivityinstance naturally destroyed, there is no so-called "no need to recreate." Press the Home button, then open the app mainactivity, in another activity stack app mainactivity into the stack, at this time startactivity to Shareactivity, You do not need to create a new shareactivity instance to see the results, because the system will find it automatically, and the presence is directly exploited. At this time, the first press Back,shareactivity instance out of the stack, at this time there is no other activity in this stack, naturally back to the app mainactivity the stack and show the app Mainactivity, then press the home button, At this point the app Mainactivity's stack also has no other activity, and does not contain any other "recent stacks", naturally back to the mobile phone desktop. Note: The "recent stacks" that are understood here are separated by the home key or the desktop state.

Simply understood, the activity identified by SingleInstance, when started, the system will first determine whether the other stack in the system already exists in the activity instance, there is a direct use, and its activity stack is theoretically only one activity element. So it's not in a task to start its activity, so pay special attention to the back question. Generally expressed as: Task1 A, Task2 B.

SingleInstance indicates that the activity is "instance unique" within the system scope. From this we find that singinstance and singletask differ primarily in the system-wide "instance unique" or the current activity stack "instance unique".

"Turn" Android Summary series: Activity startup mode (Lauchmode)

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.