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

How to use singleTask of Activity

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

Share

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

This article mainly explains "how to use singleTask in Activity". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use singleTask in Activity".

The first step is to get the Flags of the Intent used to launch Activity and save it in the launchFlags variable.

Here, the Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP bit of launcFlags is not set.

Therefore, notTop is null.

The next if statement:

[java] view plaincopy if (sourceRecord = = null) {. } else if (sourceRecord.launchMode = = ActivityInfo.LAUNCH_SINGLE_INSTANCE). } else if (r.launchMode = = ActivityInfo.LAUNCH_SINGLE_INSTANCE | | r.launchMode = = ActivityInfo.LAUNCH_SINGLE_TASK) {/ / The activity being started is a single instance... It always / / gets launched into its own task. LaunchFlags | = Intent.FLAG_ACTIVITY_NEW_TASK;}

The type of variable r here is ActivityRecord, which represents the Activity that is about to be started, in this case, SubActivity, so the r.launchMode here is equal to ActivityInfo.LAUNCH_SINGLE_TASK, so unconditionally setting the Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP position of launchFlags to 1 indicates that the SubActivity will be started in the new task, but don't worry, you have to see if other conditions are met, if all conditions are met. To start this SubActivity in a new task.

Next, initialize the addingToTask variable to false, which will also determine whether to start SubActivity in the new task. As we can see from the name, it is not added to the original task by default, that is, it should be started in the new task. Here the r.launchMode = = ActivityInfo.LAUNCH_SINGLE_TASK bar holds, and so does the condition r.resultTo = = null, which indicates that the Activity does not need to return the result to the Activity that started it. So go into the following if statement and execute:

[java] view plaincopy ActivityRecord taskTop = r.launchMode! = ActivityInfo.LAUNCH_SINGLE_INSTANCE? FindTaskLocked (intent, r.info): findActivityLocked (intent, r.info)

The condition r.launchMode! = ActivityInfo.LAUNCH_SINGLE_INSTANCE holds, so the findTaskLocked function is executed, which is also defined in the frameworks/base/services/java/com/android/server/am/ActivityStack.java file:

[java] view plaincopypublic class ActivityStack {. / * * Returns the top activity in any existing task matching the given * Intent. Returns null if no such task is found. * / private ActivityRecord findTaskLocked (Intent intent, ActivityInfo info) {ComponentName cls = intent.getComponent (); if (info.targetActivity! = null) {cls = new ComponentName (info.packageName, info.targetActivity);} TaskRecord cp = null; final int N = mHistory.size (); for (int I = (ActivityRecord) mHistory.get (I); I > = 0; iMuk -) {ActivityRecord r = (ActivityRecord) mHistory.get (I) If (! r.finishing & & r.task! = cp & & r.launchMode! = ActivityInfo.LAUNCH_SINGLE_INSTANCE) {cp = r.task; / / Slog.i (TAG, "Comparing existing cls=" + r.task.intent.getComponent (). FlattenToShortString () / + "/ aff=" + r.task.affinity + "to new cls=" / / + intent.getComponent (). FlattenToShortString () + "/ aff=" + taskAffinity) If (r.task.affinity! = null) {if (r.task.affinity.equals (info.taskAffinity)) {/ / Slog.i (TAG, "Found matching affinity!"); return r;} else if (r.task.intent! = null & & r.task.intent.getComponent (). Equals (cls)) {/ / Slog.i (TAG, "Found matching class!"); / / dump () / / Slog.i (TAG, "For Intent" + intent + "bringing to top:" + r.intent); return r;} else if (r.task.affinityIntent! = null & & r.task.affinityIntent.getComponent (). Equals (cls)) {/ / Slog.i (TAG, "Found matching class!"); / / dump () / / Slog.i (TAG, "For Intent" + intent + "bringing to top:" + r.intent); return r;} return null;}. } Thank you for your reading, the above is the content of "how to use Activity singleTask". After the study of this article, I believe you have a deeper understanding of how to use Activity singleTask, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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