Android Basics Summary Three: Four kinds of activity launcher Mode

Source: Internet
Author: User

Ext.: http://m.blog.csdn.net/blog/shift_wwx_2010/9225951

••••••••••••••••••••••••••••••••••••••••••••••••••••••••

Foreword: This article reference more information, I think this launcher mode is more key, I will try to perfect and perfect.

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.shift.launchermode;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button;ImportAndroid.widget.TextView;Importandroid.app.Activity;Importandroid.content.Intent; Public classFirstactivityextendsactivity{@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.first); TextView Show=(TextView) Findviewbyid (r.id.first_show); Show.settext ( This. toString ()); Button Go=(Button) Findviewbyid (R.ID.FIRST_GO); Go.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {gotoself ();    }        }); }        Private voidgotoself () {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.

Of course, you can also switch between two activity, you will find the serial number is also different, if first in start second, in second start first, for example, according to 1-2-1-2, then you will find that the serial number is not possible to repeat.

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:

 PackageCom.shift.launchermode;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button;ImportAndroid.widget.TextView;Importandroid.app.Activity;Importandroid.content.Intent; Public classSecondactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.second); TextView Show=(TextView) Findviewbyid (r.id.second_show); Show.settext ( This. toString ()); Button Go=(Button) Findviewbyid (R.ID.SECOND_GO); Go.setonclicklistener (NewOnclicklistener () {@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.

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:

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.

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>  

Then we start the activity in other applications like this:

New Intent ("Android.intent.action.SINGLE_INSTANCE_SHARE");  

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:

•••••••••••••••••••••••••••••••••••••••••••••••••••

It's too long to look at the summary article:

Standard startup mode, a new instance is generated, regardless of whether there are any existing instances.

Singletop startup mode, if a corresponding activity instance is found to be at the top of the stack, it is reused and no new instances are generated.

Singletask mode, if a corresponding activity instance is found, the other activity instances above this activity instance are all out of the stack, so that the activity instance becomes the top object and appears before the screen.

The singleinstance mode is special because it enables a new stack structure, places acitvity in the new stack structure, and guarantees that no other activity instances will enter.

Android Basics Summary Three: Four kinds of activity Launcher Mode (RPM)

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.