Android Activity Initiation Mode Comprehensive analysis _android

Source: Internet
Author: User

In Android, there are 4 activation modes for activity, respectively:

"Standard" (default)

"Singletop"

"Singletask"

"SingleInstance"

In Android applications, activity is the core component, how to generate an activity instance, you can choose a different startup mode, i.e. Launchmode. The starting mode mainly includes: Standard, Singletop, Singletask, SingleInstance.

Standard mode creates an instance each time it is started; Three singleton modes that choose whether to create or reuse an instance, depending on the situation. In activity initiation, the lifecycle of an instance is created: OnCreate-> onStart-> onresume; Reuse the lifecycle of an instance: Onnewintent-> onresume.

In androidmanifest activity, use the Launchmode property, you can set the startup mode, default is Standard mode; Using the Taskaffinity property and adding a package name, you can set up the activity stack, which defaults to the current package name and applies only to single mode.

In this paper, we hope to better understand the starting mode of activity (Launchmode).

Comprehensive analysis of Activity Initiation mode (Launchmode)

Watch the script for the activity stack, refer to the 5th.

ADB Shell Dumpsys Activity | Sed-n-E '/stack #/p '-e '/running activities/,/run #0/P '

1. Standard

Standard mode, start the activity of the default mode, the activated activity will run on the start of the activity stack, so you must use the context of the activity to start, can not use application, otherwise it will be an error.

such as Mainactivity start testaactivity.

Stack #1:

Running activities (most recent i):
taskrecord{3caa65e3 #2711 A=me.chunyu.spike.wcl_activity_launchmode_demo U=0 sz=2}
Run #1: Activityrecord{36b06e99 u0 me.chunyu.spike.wcl_activity_launchmode_demo/. Testaactivity t2711}
Run #0: activityrecord{27396226 u0 me.chunyu.spike.wcl_activity_launchmode_demo/. Mainactivity t2711}
Stack #0:
Running Activities (most recent):
taskrecord{27d796c9 #2695 a= Com.miui.home u=0 sz=1}
Run #0: ACTIVITYRECORD{2E5712CB u0 com.miui.home/.launcher. Launcher t2695}

Stack inside from bottom to top: mainactivity-> testaactivity.

2. Singletop

Stack top reuse mode. Only the activity is on the top of the stack, and the default instance, the singleton mode, is used when the recurrence starts. If located inside the stack, the instance is still created.

Mainactivity starts Testa, Testa starts Testb, TESTB initiates itself, and TESTB is a single example. Observe the situation in the stack, TESTB only one instance, the second time to create a reuse.

Stack #1:

Running activities (most recent i):
taskrecord{12abf566 #2712 A=me.chunyu.spike.wcl_activity_launchmode_demo U=0 sz=3}
Run #2: Activityrecord{187d7ff7 u0 me.chunyu.spike.wcl_activity_launchmode_demo/. Testbactivity t2712}
Run #1: activityrecord{a551034 u0 me.chunyu.spike.wcl_activity_launchmode_demo/. Testaactivity t2712}
Run #0: Activityrecord{22f9cce4 u0 me.chunyu.spike.wcl_activity_launchmode_demo/. Mainactivity t2712}

Stack: mainactivity-> testaactivity-> testbactivity

mainactivity boot testa, testa boot testb, testb boot testc, TESTC boot Testb, TESTB is a single example. Observe the situation in the stack, because TESTC is the top of the stack, TESTC start Testb, TESTB is not the top of the stack, recreate the Testb instance, then keep two copies of Testb.

Stack #1:

Running activities (most recent i):
taskrecord{1792f5f0 #2715 A=me.chunyu.spike.wcl_activity_launchmode_demo U=0 sz=5}
Run #4: activityrecord{1e70110b u0 me.chunyu.spike.wcl_activity_launchmode_demo/. Testbactivity t2715}
Run #3: Activityrecord{c7f4dce u0 me.chunyu.spike.wcl_activity_launchmode_demo/. Testcactivity t2715}
Run #2: ACTIVITYRECORD{254536CD u0 me.chunyu.spike.wcl_activity_launchmode_demo/. Testbactivity t2715}
Run #1: Activityrecord{36b2da15 u0 me.chunyu.spike.wcl_activity_launchmode_demo/. Testaactivity t2715}
Run #0: activityrecord{3a1c4a6a u0 me.chunyu.spike.wcl_activity_launchmode_demo/. Mainactivity t2715}

Stack: mainactivity-> testaactivity-> testbactivity->testcactivity-> testbactivity

3. Singletask

In-stack reuse mode, as long as the activity is present in a stack, the instance is not created when multiple calls are made, that is, the singleton mode.

The situation includes the following types:

(1) The task stack does not exist, the initial start Singletask instance, will create the task stack and instance.

Mainactivity starts Testa, Testa starts Testb, Testb is Singletask, and the task stack is different. It is known that the system contains two task stacks, TESTB on other task stacks.

Stack #1:

Running activities (most recent a):
taskrecord{d5d53d4 #2727 a=me.chunyu.spike.stack u=0 sz=1}
Run #2: Activityrecord{1d720e55 u0 me.chunyu.spike.wcl_activity_launchmode_demo/. Testbactivity t2727}
taskrecord{a3f797d #2726 a=me.chunyu.spike.wcl_activity_launchmode_demo U=0 sz=2}
Run # 1:activityrecord{ffd689d u0 me.chunyu.spike.wcl_activity_launchmode_demo/. Testaactivity t2726}
Run #0: activityrecord{192310ac u0 me.chunyu.spike.wcl_activity_launchmode_demo/. Mainactivity t2726}

Using the Taskaffinity property, add a new activity stack, which is used in conjunction with Singletask, and the standard mode is invalid.

The new task stack is me.chunyu.spike.stack.

(2) The task stack exists, the initial start Singletask instance, will directly into the stack, and standard mode the same.

(3) The same task stack, start singletask instance, the instance will be placed on the top of the stack, and clear the above instance, with cleartop effect.

Mainactivity starts Testa, Testa starts Testb, Testb is Singletask, Testb starts TESTC, TESTC restarts Testb, then TESTC stacks up. Observation that testc out of the stack, TESTB located on the top of the stack.

Stack #1:

Running activities (most recent i):
taskrecord{18230815 #2737 A=me.chunyu.spike.wcl_activity_launchmode_demo U=0 sz=3}
Run #4: activityrecord{1126c300 u0 me.chunyu.spike.wcl_activity_launchmode_demo/. Testbactivity t2737}
Run #3: Activityrecord{3114fee8 u0 me.chunyu.spike.wcl_activity_launchmode_demo/. Testaactivity t2737}
Run #2: activityrecord{f8e235d u0 me.chunyu.spike.wcl_activity_launchmode_demo/. Mainactivity t2737}

TESTC start Testb, Singletask mode, lead to Cleartop, TESTC out stack.

(4) The task stack is different, start singletask instance again, can cause task stack to switch, backstage puts in front desk.

This is more difficult to understand. mainactivity boot testa, testa boot testb (singletask instance, different task stacks), TESTB boot TESTC (similar to B), then mainactivity and testa the same stack, TESTB and TESTC the same stack, At this time the top of the stack is TESTC. Press the home key to start the application again, the default task stack starts, Testa starts, and Testa starts TESTC. The current status of the application is as follows.

Stack #1:

Running activities (most recent a):
taskrecord{1d05e6c9 #2754 a=me.chunyu.spike.stack u=0 sz=2}
Run #4: activityrecord{3f77e822 u0 me.chunyu.spike.wcl_activity_launchmode_demo/. Testcactivity t2754}
taskrecord{3fe736d0 #2753 a=me.chunyu.spike.wcl_activity_launchmode_demo U=0 sz=2}
Run #3: activityrecord{15f0470e u0 me.chunyu.spike.wcl_activity_launchmode_demo/. Testaactivity t2753}
taskrecord{1d05e6c9 #2754 a=me.chunyu.spike.stack u=0 sz=2}
Run #2: activityrecord{ 181229e6 u0 me.chunyu.spike.wcl_activity_launchmode_demo/. Testbactivity t2754}
taskrecord{3fe736d0 #2753 a=me.chunyu.spike.wcl_activity_launchmode_demo U=0 sz=2}
Run #1: activityrecord{28628d61 u0 me.chunyu.spike.wcl_activity_launchmode_demo/. Mainactivity t2753}
taskrecord{2d646058 #2719 a=com.android.incallui u=0 Sz=1}

TESTC as to the top of the stack, click on the back key, not return Testa (start TESTC instance), but Testb, that is, first return the same stack instance. Again is Testa, then is mainactivity, sequentially out stack.

4. SingleInstance

Single instance mode, when started, the system creates a separate task stack for it, which is used for each subsequent use, until it is destroyed and belongs to the real singleton pattern.

Example: Mainactivity starts Testa, Testa starts Testb (singleinstance mode),

Testb starts the TESTC, TESTC starts the TESTB, then the previous TESTB is still started.

TESTC Merge into the default stack (Mainactivity+testa).

Stack #1:

Running activities (most recent i):
taskrecord{384e3928 #2765 A=me.chunyu.spike.wcl_activity_launchmode_demo U=0 sz=1}
Run #3: activityrecord{1ffc5b6b u0 me.chunyu.spike.wcl_activity_launchmode_demo/. Testbactivity t2765}
taskrecord{2ad03544 #2764 a=me.chunyu.spike.wcl_activity_launchmode_demo U=0 sz=3}
Run #2: Activityrecord{293d8c37 u0 me.chunyu.spike.wcl_activity_launchmode_demo/. Testcactivity t2764}
Run #1: activityrecord{158bc0f3 u0 me.chunyu.spike.wcl_activity_launchmode_demo/. Testaactivity t2764}
Run #0: activityrecord{77691cf u0 me.chunyu.spike.wcl_activity_launchmode_demo/. Mainactivity t2764}

5. Startactivityforresult

The happy time of thx@ callback

Startactivityforresult Unlike StartActivity, when you start an activity using Launchmode mode, there are also different ways to pass data normally, but when you cannot create your own, multiple instances are generated.

When TESTB (Singletask mode) uses StartActivity to create itself, it uses the default instance, that is, a single example; When you create yourself using Startactivityforresult, a new example is generated.

Stack #1:

Running activities (most recent a):
taskrecord{323200ac #2786 a=me.chunyu.clwang.stack u=0 sz=3}
Run #4: Activityrecord{3f9e14f3 u0 me.chunyu.spike.wcl_activity_launchmode_demo/. Testbactivity t2786}
Run #3: activityrecord{30d8f17b u0 me.chunyu.spike.wcl_activity_launchmode_demo/. Testbactivity t2786}
Run #2: activityrecord{11b95b5c u0 me.chunyu.spike.wcl_activity_launchmode_demo/. Testbactivity t2786}
taskrecord{c86e175 #2785 a=me.chunyu.spike.wcl_activity_launchmode_demo U=0 sz=2}
Run # 1:activityrecord{3558d7c4 u0 me.chunyu.spike.wcl_activity_launchmode_demo/. Testaactivity t2785}
Run #0: activityrecord{1b8620c u0 me.chunyu.spike.wcl_activity_launchmode_demo/. Mainactivity t2785}

As a result, because Startactivityforresult needs to return a value, the instance is preserved and the singleton effect is overwritten.

Note: The 4.x version starts with the Startactivityforresult Singletask and does not get the return value normally, reference.

5.x above fixes this issue, consider compatibility, do not recommend the use of Startactivityforresult and Singletask.

Starting mode as an important attribute of activity, or need to be more thorough mastery.

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.