Android--activity Four types of startup modes

Source: Internet
Author: User

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.

<pre name= "code" class= "HTML" style= "FONT-SIZE:14PX;" ><activity      android:name= ". A1 "      
Standard
the default mode, which can be configured without writing. In this mode, a new instance will be created by default. Therefore, in this mode, you can have multiple identical instances, and also allow multiple identical activity overlays.

For example:
If I have an activity named A1, there is a button to jump to A1. Then if I click on the button, it will start a new activity A1 stacked on the A1 just above, then click on it, and then City one on it ...
Click the back key to exit in the order of the stack.
Singletop
There can be multiple instances, but multiple identical activity overlays are not allowed. That is, if the activity is at the top of the stack, the same activity is started, the new instance is not created, and its Onnewintent method is called.

For example:
If I have two activity named B1,B2, two activity content function is identical, there are two buttons can jump to B1 or B2, the only difference is B1 to STANDARD,B2 for Singletop.
If the order in which I intend to open is b1->b2->b2, then the actual order opened is B1->B2 (the next attempt to open B2, actually only the previous Onnewintent method is called)
If the order I intend to open is b1->b2-> B1->B2, the actual order of opening is consistent with the intention, and is b1->b2->b1->b2.

role: avoid a bad user experience, if the interface has been opened and on the stack top of the task stack, it will not be repeatedly opened singletask
There is only one instance. When you launch him in the same application, if the activity does not exist, a new instance is created in the current task and, if present, the other activity on top of the task is destory off and the Onnewintent method is called.
If you start it in another application, a new task is created and started in that task Activity,singletask allows other activity to be stored in a task, that is, If I open a new activity in this Singletask instance, the new activity will still be in the task of the Singletask instance.

For example:
If there are three activity,c1,c2,c3 in my application, three activity can start each other, where C2 is Singletask mode, then no matter how I click Start in this program, such as:c1->c2->c3-> There may be multiple instances of C2->C3->C1-C2,C1,C3, but there is only one C2, and the three activity is in the same task.
but C1->C2->C3->C2->C3->C1-C2, this process should actually be as follows, because Singletask will destory the other activity on top of the task.
operation: c1->c2 c1->c2->c3 c1->c2->c3->c2 c1->c2->c3->c2->c3-> C1 C1->C2->C3->C2->C3->C1-C2
actual: c1->c2 c1->c2->c3 c1->c2 c1->c2->c3->c1 C1->C2

If another application opens C2, it will start a new task.
If another application has a activity,taskid of 200, from which it opens C2, then C2 Taskidi will not be 200, for example C2 TaskID is 201, then C2, C1 from C3, C2, C3 's TaskID is still 201.
Note: If you click Home and then open other, you'll see that it will certainly be the content in the other app, not one of the C1 C2 C3 in our app.

Application Scenarios:

          Browser: The bottom is using the webkit c  kernel, the initialization needs to request a lot of memory resources, CPU time, so use Singletask to ensure that only one instance exists in the task stack

SingleInstance

There is only one instance, and this instance runs independently in a task, and this task is the only instance that does not allow any other activity to exist.

For example:
The program has three activityd1,d2,d3, three activity can start each other, wherein D2 is singleinstance mode. Then the program starts running from D1, assuming that the taskid of D1 is 200, then D2 starts a new task when D2 is started from D1, that is, D2 and D1 are not running in a task. Assuming that D2 has a taskid of 201, and then starts D3 from D2, D3 TaskID is 200, which means it is pressed into the D1-initiated task stack.

If you open D2 in another application, assuming that other's taskid is 200, open d2,d2 will create a new task run, assuming its taskid is 201, then if you start D2 or D1 from D3, then another task is created, so If the operation step is OTHER->D2->D1, the process involves 3 tasks.

Characteristics:

SingleInstance's boot mode is more extreme,

Start a new activity and create a separate task stack for yourself

Whether it's open from within the app or by another app

The taskid is separate, and already exists, just call onnewintent


Application Scenarios:

There will only be one instance of the activity in the entire mobile operating system,

Youdao Dictionary, Jinshan Dictionary

So multiple applications share this activity instance, thread security issues!

such as alarm reminders, separating alarm alarms from alarm settings


Android--activity Four types of startup modes

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.