Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Activity Startup Mode

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces the example analysis of Activity startup mode, which is very detailed and has certain reference value. Friends who are interested must finish it!

In Android applications, Activity is the core component. How to generate an Activity instance, you can choose different startup modes, namely LaunchMode. Startup modes mainly include: standard, singleTop, singleTask, singleInstance.

The standard mode creates an instance each time it is started, and the three singleton modes choose whether to create or reuse the instance according to the situation. In Activity startup, create instance lifecycle: onCreate-> onStart-> onResume; reuse instance lifecycle: onNewIntent-> onResume.

In the Activity of AndroidManifest, using the launchMode attribute, you can set the startup mode, which defaults to the standard mode; using the taskAffinity attribute and adding the package name, you can set the Activity stack, which defaults to the current package name and can only be applied to the single mode.

Observe the script for the Activity stack.

Adb shell dumpsys activity | sed-n-e'/ Stack # / p'- e'/ Running activities/,/Run # 0max p'

Standard

Standard mode, the default mode of starting Activity, the started Activity will run on the started Activity stack, so you must use Activity's Context to start, can not use Application, otherwise an error will be reported.

For example, MainActivity starts TestAActivity.

Stack # 1: Running activities (most recent first): TaskRecord {3caa65e3 # 2711 A=me.chunyu.spike.wcl_activity_launchmode_demo Ubun0 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 first): TaskRecord {27d796c9 # 2695 A=com.miui.home Ubuno sz=1} Run # 0: ActivityRecord {2e5712cb u0 com.miui.home/.launcher.Launcher t2695}

Stack from bottom to top: MainActivity-> TestAActivity.

SingleTop

Top stack reuse mode. Only Activity is at the top of the stack. When starting repeatedly, the default instance, singleton mode, will be used; if it is in the stack, the instance will still be created.

MainActivity starts TestA, TestA starts TestB, TestB starts itself, and TestB is a single example. Observe the situation in the stack, there is only one instance of TestB, the second time to create reuse.

Stack # 1: Running activities (most recent first): TaskRecord {12abf566 # 2712 A=me.chunyu.spike.wcl_activity_launchmode_demo Ubun0 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}

On-stack: MainActivity-> TestAActivity-> TestBActivity

MainActivity starts TestA, TestA starts TestB, TestB starts TestC, TestC starts TestB, and TestB is a single case. Observe the situation in the stack, because TestC is the top of the stack, TestC starts TestB, and TestB is not the top of the stack. If you recreate the TestB instance, keep two copies of TestB.

Stack # 1: Running activities (most recent first): TaskRecord {1792f5f0 # 2715 A=me.chunyu.spike.wcl_activity_launchmode_demo Ubun0 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}

On-stack: MainActivity-> TestAActivity-> TestBActivity-> TestCActivity-> TestBActivity

SingleTask

In-stack reuse mode, as long as Activity exists in a stack, no instance will be created when it is called many times, that is, singleton pattern.

Situations include the following:

(1) Task stack does not exist. Starting a SingleTask instance for the first time will create a task stack and an instance.

MainActivity starts TestA, TestA starts TestB, TestB is SingleTask, and the task stack is different. Observation shows that the system consists of two task stacks, and TestB is located in other task stacks.

Stack # 1: Running activities (most recent first): TaskRecord {d5d53d4 # 2727 A=me.chunyu.spike.stack Ubun0 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 Utt0 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}

Use the taskAffinity attribute to add a new Activity stack. With SingleTask, Standard mode is invalid.

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

(2) the task stack exists. If you start the SingleTask instance for the first time, it will enter the stack directly, which is the same as the Standard mode.

(3) the task stack is the same. If you start the SingleTask instance again, the instance will be placed on the top of the stack and cleared, which has the effect of clearTop.

MainActivity starts TestA, TestA starts TestB, TestB is SingleTask, TestB starts TestC, TestC restarts TestB, then TestC will be out of stack. Observation shows that TestC is out of stack and TestB is at the top of the stack.

Stack # 1: Running activities (most recent first): TaskRecord {18230815 # 2737 A=me.chunyu.spike.wcl_activity_launchmode_demo Ubun0 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 starts TestB, SingleTask mode, causing clearTop and TestC to go out of stack.

(4) if the task stack is different, starting the SingleTask instance again will cause the task stack to switch and the background will be placed in the foreground.

It is difficult to understand. MainActivity starts TestA, TestA starts TestB (SingleTask instance, different task stack), TestB starts TestC (similar to B), then MainActivity and TestA have the same stack, TestB and TestC have the same stack, and the top of the stack is TestC. Press the home key to start the application again, and the default task stack will start, TestA will start, and TestA will start TestC. The current status of the application is as follows.

Stack # 1: Running activities (most recent first): TaskRecord {1d05e6c9 # 2754 A=me.chunyu.spike.stack Ubun0 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 Utt0 sz=2} Run # 3: ActivityRecord {15f0470e u0 me.chunyu.spike.wcl_activity_launchmode_demo/.TestAActivity t2753} TaskRecord {1d05e6c9 # 2754 A=me.chunyu.spike.stack Ubun0 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 UBO sz=2} Run # 1: ActivityRecord {28628d61 U0 me.chunyu.spike.wcl_activity_launchmode_demo/.MainActivity t2753} TaskRecord {2d646058 # 2719 A=com.android.incallui UBO sz=1}

TestC as for the top of the stack, click the back key, not return TestA (start the instance of TestC), but TestB, that is, give priority to return the instance of the same stack. Then TestA, then MainActivity, then out of the stack.

SingleInstance

When the single instance mode is started, the system will create a separate task stack for it, and this singleton will be used every time it is used until it is destroyed, which belongs to the real singleton mode.

Example: MainActivity starts TestA, TestA starts TestB (SingleInstance mode)

If TestB starts TestC and TestC starts TestB, the last TestB will still be started.

TestC is merged into the default stack (MainActivity+TestA).

Stack # 1: Running activities (most recent first): TaskRecord {384e3928 # 2765 A=me.chunyu.spike.wcl_activity_launchmode_demo Ubun0 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 UBO 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}

StartActivityForResult

StartActivityForResult is different from startActivity. When you start Activity in LaunchMode mode, you can transfer data normally, but multiple instances will be generated when you cannot create your own continuously.

When TestB (singleTask mode) creates itself using startActivity, it uses the default instance, that is, singleton, and when it creates itself with startActivityForResult, it generates a new example.

Stack # 1: Running activities (most recent first): TaskRecord {323200ac # 2786 A=me.chunyu.clwang.stack Ubun0 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 Ubun0 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}

Therefore, because startActivityForResult needs to return a value, it retains the instance and overrides the singleton effect.

Note: version 4.x starts singleTask through startActivityForResult and cannot get the return value normally, reference.

Version 5.x or above fixes this problem, considering compatibility, and startActivityForResult and singleTask are not recommended.

Intent sets flag bit

Intent can set the startup flag bit, namely Flag.

Intent.addFlags (Intent.FLAG_ACTIVITY_NEW_TASK)

AndroidManifest cannot set FLAG_ACTIVITY_CLEAR_TOP, that is, clears other instances on the stack; Intent cannot set singleInstance startup mode. You can use either of the two. If you use both, the priority of Intent is higher than that of AndroidManifest.

Commonly used flag bits:

FLAG_ACTIVITY_NEW_TASK: same as singleTask startup mode.

FLAG_ACTIVITY_SINGLE_TOP: same as singleTop startup mode.

FLAG_ACTIVITY_CLEAR_TOP: general and singleTask startup mode appear. If it is singleTask startup mode, it will clear other instances on the stack, reuse instances, call onNewIntent;. If it is standard startup mode, that is, the default mode, it will clear itself and other instances, and re-create it, calling onCreate.

Display the Shell command of the stack

Shell command

Adb shell dumpsys activity | sed-n-e'/ Stack # / p'- e'/ Running activities/,/Run # 0max p'

Getting Activity information directly is a bit redundant, we can only focus on stack information.

Sed can edit the displayed text.

-n, start continuous processing from the intercept.

-e, select more parameters.

'/ Stack # / p', output the line containing Stack #.

-e'/ Running activities/,/Run # 0 Running activities/,/Run p', which outputs all lines from Running activities to Run # 0.

Output result

Stack # 1: Running activities (most recent first): TaskRecord {299f41ea # 2269 A=me.chunyu.spike.wcl_activity_launchmode_demo Ubun0 sz=6} Run # 5: ActivityRecord {33926043 u0 me.chunyu.spike.wcl_activity_launchmode_demo/.MainActivity t2269} Run # 4: ActivityRecord {3f181566 u0 me.chunyu.spike.wcl_activity_launchmode_demo/.MainActivity t2269} Run # 3: ActivityRecord {22737e45 u0 me.chunyu.spike.wcl_activity_ Launchmode_demo/.MainActivity t2269} Run # 2: ActivityRecord {ce0a990 u0 me.chunyu.spike.wcl_activity_launchmode_demo/.MainActivity t2269} Run # 1: ActivityRecord {3de8e378 u0 me.chunyu.spike.wcl_activity_launchmode_demo/.MainActivity t2269} Run # 0: ActivityRecord {1cb28ec4 u0 me.chunyu.spike.wcl_activity_launchmode_demo/.MainActivity t2269} Stack # 0: Running activities (most recent first): TaskRecord {bfee9cf # 2241 A=com.miui.home Ubuno sz=1} Run # 0: ActivityRecord {279bc098 u0 com.miui.home/.launcher.Launcher t2241}

Boot mode, as an important attribute of Activity, still needs to be thoroughly mastered.

The above is all the contents of the article "sample Analysis of Activity Startup Mode". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report