Four launchmodes of Android activity !!!

Source: Internet
Author: User

This article from: http://marshal.easymorse.com/archives/2950. write very good, share with you !!!

In multi-activity development, activity jumps between applications or reusable activities with other applications. You may want to jump to an existing activity instance instead of generating a large number of duplicate activities.

This requires configuring a specific loading mode for the activity, rather than using the default loading mode.

Loading Mode classification and where to configure

Activity has four loading modes:

  • Standard
  • Singletop
  • Singletask
  • Singleinstance

The set location is in the Android: launchmode attribute of the activity element in the androidmanifest. xml file:

<Activity Android: Name = "actb"Android: launchmode
= "Singletask"> </activity>

You can also edit it in the eclipse ADT graphic interface:

 

Differentiate the loading mode of the activity. Here is an example of loop jump between activity A (ACTA) and activity B (actb. The differences between the four modes can be described by slightly modifying the loading mode and code.

Standard

First, the standard mode is the default mode. You do not need to configure the launchmode. First, write only one activity named ACTA:

Package COM. easymorse. activities; <br/> Import android. app. activity; <br/> Import android. content. intent; <br/> Import android. OS. bundle; <br/> Import android. view. view; <br/> Import android. view. view. onclicklistener; <br/> Import android. widget. button; <br/> Import android. widget. linearlayout; <br/> Import android. widget. textview; <br/> public class ACTA extends activity {<br/>/** called when the activity is first created. */<br/> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> textview = new textview (this); <br/> textview. settext (This + ""); <br/> button = new button (this); <br/> button. settext ("Go Acta"); <br/> button. setonclicklistener (New onclicklistener () {<br/> @ override <br/> Public void onclick (view v) {<br/> intent = new intent (); <br/> intent. setclass (ACTA. this, ACTA. class); <br/> startactivity (intent); <br/>}< br/>}); <br/> linearlayout layout = new linearlayout (this ); <br/> layout. setorientation (linearlayout. vertical); <br/> layout. addview (textview); <br/> layout. addview (button); <br/> This. setcontentview (layout); <br/>}< br/>

Layout is not used in the example, so that you do not have to look at it. See the example of ACTA-> ACTA. Print the tostring value of the object on the interface to identify whether to create a new ACTA instance based on the hash code.

First interface:

Click the button:

It can be multiple times. It is found that a new instance of the activity is created every time. The loading mode of standard is like this, and intent will send to the new instance.

Click the rollback key of the Android device. The result is displayed in descending order of the activity instance you just created, similar to the rollback operation. The procedure of the jump button is the stack operation. For example:

Singletop

Both singletop and standard modes send intent instances (the latter two modes do not send intent instances, if they already exist ). No
Yes. singletop requires that if there is an activity instance at the top of the stack when intent is created, the intent will be sent to the instance instead of the new instance.

In the preceding example, you only need to change the launchmode to singletop to see the difference.

During running, you will find that the buttons are the same ACTIA instance, because the instance is at the top of the stack, so no new instance will be created. If it is rolled back, the application will exit.

Singletop mode can be used to solve the problem of multiple identical activities at the top of the stack.

If a activity jumps to Activity B and then to activity A, the behavior is the same as that of standard. A new instance of Activity A is created when Activity B jumps to activity, because the stack top is not a activity instance at that time.

Slightly changed the ACTA class:

Package COM. easymorse. activities; <br/> Import android. app. activity; <br/> Import android. content. intent; <br/> Import android. OS. bundle; <br/> Import android. view. view; <br/> Import android. view. view. onclicklistener; <br/> Import android. widget. button; <br/> Import android. widget. linearlayout; <br/> Import android. widget. textview; <br/> public class ACTA extends activity {<br/>/** called when the activity is first created. */<br/> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> textview = new textview (this); <br/> textview. settext (This + ""); <br/> button = new button (this); <br/> button. settext ("Go actb"); <br/> button. setonclicklistener (New onclicklistener () {<br/> @ override <br/> Public void onclick (view v) {<br/> intent = new intent (); <br/> intent. setclass (ACTA. this, actb. class); <br/> startactivity (intent); <br/>}< br/>}); <br/> linearlayout layout = new linearlayout (this ); <br/> layout. setorientation (linearlayout. vertical); <br/> layout. addview (textview); <br/> layout. addview (button); <br/> This. setcontentview (layout); <br/>}< br/>}

 

Actb class:

Package COM. easymorse. activities; <br/> Import android. app. activity; <br/> Import android. content. intent; <br/> Import android. OS. bundle; <br/> Import android. view. view; <br/> Import android. view. view. onclicklistener; <br/> Import android. widget. button; <br/> Import android. widget. linearlayout; <br/> public class actb extends activity {<br/> @ override <br/> protected void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> button = new button (this); <br/> button. settext ("Go Acta"); <br/> button. setonclicklistener (New onclicklistener () {<br/> @ override <br/> Public void onclick (view v) {<br/> intent = new intent (); <br/> intent. setclass (actb. this, ACTA. class); <br/> startactivity (intent); <br/>}< br/>}); <br/> linearlayout layout = new linearlayout (this ); <br/> layout. addview (button); <br/> This. setcontentview (layout); <br/>}< br/>}

 

The actb class is loaded by default (standard), and ACTA is loaded by singletop. The result is similar:

If you change the ACTA loading mode to standard, the situation is the same.

Singletask

The singletask mode and the singleinstance mode are used to create only one instance.

When intent arrives and a singletask activity needs to be created, the system checks whether there are instances of the activity in the stack. If any, send intent directly to it.

Change the launchmode of ACTA in the singletop instance to singletask, and change actb to standard. Click the button once on the ACTA interface:

Click the button on the actb1 interface. Because ACTA is a singletask, the original acta1 instance is used. At this time, the situation in the stack:

If you press the button multiple times, you will find that only the ACTA instance of the acta1.

Singleinstance

It is difficult to explain the singleinstance mode.

First, let's talk about the concept of task.

If it is a swing or Windows program, there may be multiple Windows that can be switched, but you cannot reuse the windows in your program. Note that the binary code is reused directly, instead of the source code-level calling after you get the API.

Android can achieve this by allowing other programs to directly reuse your activity (similar to windows of desktop programs ).

Android introduces the concept of task to provide this mechanism. A task can be considered as a stack and put into multiple activities. For example, to start an application
Android creates a task and starts the entry activity of the application, which is the one configured as main and launch in intent-filter.
(See the effect of deploying an APK file to install multiple applications.
). This activity is a root activity and may call other activities on its interface. If these activities follow the preceding three modes, they will also be in this stack (task, the instantiation policy is different.

The verification method is to call and print the taskid of the activity:

Textview textview2 = new textview (this );
Textview2.settext ("task id:" + this. gettaskid ());

You will find that the taskid is the same no matter the activity is switched.

Of course, you can also put other people's activities in this single task stack, such as Google map. This way, when you see the map and press the rollback key, the stack will return to the activity that calls the map. Users do not think they are operating on multiple applications. This is the role of the task.

However, multiple tasks share an activity (singletask shares an activity in a task ).

An example is Google map. For example, I have an app for tour guides, in which Google map activity is called. Now, for example, press the Home Key and open Google map in the Application List. You will find that the map just now is actually the same activity.

If the above three modes are used, this requirement cannot be met. The Google map app has multiple context activities, such as route queries. The app also has some context activities. In each application, you must roll back to the corresponding context activity.

The singleinstance mode solves this problem (the question is answered after such a long time ). Let the activity in this mode be independent in a task stack. This stack has only one activity. The intent sent by the tour guide application and Google map application is received and displayed by this activity.

There are two more problems:

  • In this case, Multiple Task stacks can also be considered as one application. For example, when the tour guide application starts a map activity, it is actually on the task stack of the tour guide application.
    A new stack is created in singleinstance mode (if not, it is directly displayed if any). When the unique activity in the stack is, the map Activity
    When I rolled back, I just removed the stack, so that I could see the activity just now applied to the tour guide;
  • When multiple applications (tasks) share an activity, these applications do not exit. For example, the Home key is used to switch from the tour guide application to the map application. Because, if you exit the tour guide application and the map application is not running, the independent map activity (task) will also exit.

If you still use the example of ACTA and actb, you can change the mode of actb to singleinstance and ACTA to standard. If you switch to actb by clicking one button, the phenomenon is similar to this:

If the button is switched to actb for the first time, press the button on actb to switch to ACTA, and then roll back, which is:

In addition, we can see that the taskids of the two activities are different.

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.