Four kinds of startup modes for activity in Android programming _android

Source: Internet
Author: User

This example describes four startup modes for activity in Android programming. Share to everyone for your reference, specific as follows:

Activity Initiation method has four kinds, namely:

Standard
Singletop
Singletask
SingleInstance

You can set up a corresponding startup mode for the activity based on actual requirements, thereby avoiding problems such as creating a large number of duplicate activity.

Set the start mode of the activity, simply set the Android:launchmode attribute in the corresponding <activity> tab in Androidmanifest.xml, for example:

<activity 
  android:name= ". A1 " 
  android:launchmode=" standard "/> 

Here are the effects of these four modes:

Standard

Default mode, you can not write configuration. In this mode, a new instance is created by default. Therefore, in this mode, there can be multiple instances of the same, and multiple identical activity overlays are allowed.

For example:

If I have an activity named A1, there is a button on it to jump to A1. So if I click on the button, I will start a new activity A1 stacked on top of the A1, then click, and then le One on top of it ...

The point back key is followed by the stack order exit.

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, initiating the same activity, the new instance is not created, and its Onnewintent method is invoked.

For example:

If I had two activity named B1,b2, and two content functions exactly the same, two buttons could jump to B1 or B2, the only difference being standard,b2 B1.
If the order that I intend to open is b1->b2->b2, then the order of the actual opening is B1->B2 (the last attempt to open B2, which actually only calls 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 consistent with the intention, for B1->B2->B1->B2.

Singletask

Only one instance. When you start him in the same application, if the activity does not exist, a new instance is created on the current task, and if it exists, the other activity on top of the task is destory off and the Onnewintent method is invoked.

If you start it in another application, a new task is created and the Activity,singletask is started in the task to allow other activity to exist in a task, that is to say, If I open a new activity in this Singletask instance, the new activity will 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 to start in this program, such as: c1->c2->c3->c2- There may be multiple instances of >C3->C1-C2,C1,C3, but C2 only exist, and the three activity is within the same task.

But C1->C2->C3->C2->C3->C1-C2, such an operation 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 the C2, a new task is opened.

If another application other than the other has a activity,taskid of 200, from which the C2 is opened, then the Taskidi of C2 will not be 200, for example, C2 TaskID is 201, then C2, C1 from C3 is still 201.

Note: If you click Home at this time and then open other, you will see that this is definitely going to be the content of other apps, not one of the C1 C2 C3 in our application.

SingleInstance

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

For example:

The program has three activityd1,d2,d3, three activity can start each other, where D2 is SingleInstance mode. Then the program starts from D1 and assumes that D1 's TaskID is 200, and when you start D2 from D1, D2 starts a new task, that is, D2 and D1 are not running in a task. Assuming that D2 's taskid is 201, and then starts D3 from D2, D3 TaskID is 200, which means it is pressed to the D1-initiated task stack.

If you open D2 in another application, assuming that other taskid is 200, opening D2,D2 will create a new task run, assuming that its taskid is 201, then if you start D2 or D1 from the D3, then you will be creating a task again, so If the procedure is OTHER->D2->D1, the process involves 3 tasks.

For more information on Android-related content readers can view the site: "The activity of Android programming skills Summary", "Android Communication method Summary", "Android debugging techniques and common problems solution summary", " Android Introduction and advanced tutorials, summary of Android Basic components usage, Android layout layout tips and a summary of Android controls usage

I hope this article will help you with the Android program.

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.