Activity startup mode

Source: Internet
Author: User
Tags exit in

There are four ways to activate the activity, namely:

Standard
Singletop
Singletask
SingleInstance

To set the activity's startup mode, just set the Android:launchmode property in the corresponding <activity> tag in androidmanifest.xml

<activity

android:name=". A1 "

android:launchmode="Standard" />

Here are the four modes of action:

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 standard,b2 for Singletop.
If the order in which I intend to open is b1->b2->b2, then the actual order of opening is B1-&GT;B2 (the next attempt to open B2, which actually calls only the previous Onnewintent method)
If the order in which I intend to open is b1->b2->b1->b2, then the actual order of opening is the same as the intention, b1->b2->b1->b2.


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, C2 is Singletask mode, then no matter how I click Start in this program, such as: c1->c2->c3->c2- There may be multiple instances of &GT;C3-&GT;C1-C2,C1,C3, but there is only one C2, and the three activity is in the same task.
But C1-&GT;C2-&GT;C3-&GT;C2-&GT;C3-&GT;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 the other application has a activity,taskid of 200, from which you open C2, the Taskidi of C2 will not be 200, for example C2 TaskID is 201, then C2, C1 from C3, C2 C3, 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.


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-&GT;D2-&GT;D1, the process involves 3 tasks.

Activity startup mode

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.