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

What is the function of Activity's singleTask?

2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "what is the function of Activity's singleTask". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "what is the role of Activity's singleTask?"

This function simply looks in the system based on the taskAffinity attribute value of the soon-to-be-launched SubActivity to find that the affinity property value of such a Task:Task is the same as the taskAffinity attribute value of the soon-to-be-launched Activity.

If it exists, return to the Activity at the top of the Task stack.

In the above AndroidManifest.xml file, the taskAffinity attribute of MainActivity and SubActivity is not configured, so their taskAffinity attribute value defaults to the taskAffinity attribute value of the parent tag application. Here, the taskAffinity of the tag application is not configured, so they default to the package name, that is, "shy.luo.task".

Since MainActivity is started before starting SubActivity, when MainActivity starts, it starts in a new task, and the affinity property of this new task equals the taskAffinity attribute value of its * Activity.

Therefore, the function moves back to the ActivityRecord that represents MainActivity.

Going back to the previous startActivityUncheckedLocked function, taskTop here represents MainActivity, which is not null, so move on. Because the condition r.launchMode = = ActivityInfo.LAUNCH_SINGLE_TASK holds, execute the following statement:

[java] view plaincopyActivityRecord top = performClearTaskLocked (kTop.task.taskId, r, launchFlags, true)

Function performClearTaskLocked is also defined in the frameworks/base/services/java/com/android/server/am/ActivityStack.java file:

[java] view plaincopypublic class ActivityStack {. / * * Perform clear operation as requested by * {@ link Intent#FLAG_ACTIVITY_CLEAR_TOP}: search from the top of the * stack to the given task, then look for * an instance of that activity in the stack and, if found, finish all * activities on top of it and return the instance. * * @ param newR Description of the new activity being started. * @ return Returns the old activity that should be continue to be used, * or null if none was found. * / private final ActivityRecord performClearTaskLocked (int taskId, ActivityRecord newR, int launchFlags, boolean doClear) {int I = mHistory.size (); / / First find the requested task. While (I > 0) {imurmurl; ActivityRecord r = (ActivityRecord) mHistory.get (I); if (r.task.taskId = = taskId) {iMurray; break;}} / / Now clear it. While (I > 0) {imurmurl; ActivityRecord r = (ActivityRecord) mHistory.get (I); if (r.finishing) {continue;} if (r.task.taskId! = taskId) {return null;} if (r.realActivity.equals (newR.realActivity)) {/ / Here it is! Now finish everything in front... ActivityRecord ret = r; if (doClear) {while (I)

< (mHistory.size()-1)) { i++; r = (ActivityRecord)mHistory.get(i); if (r.finishing) { continue; } if (finishActivityLocked(r, i, Activity.RESULT_CANCELED, null, "clear")) { i--; } } } // Finally, if this is a normal launch mode (that is, not // expecting onNewIntent()), then we will finish the current // instance of the activity so a new fresh one can be started. if (ret.launchMode == ActivityInfo.LAUNCH_MULTIPLE && (launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) == 0) { if (!ret.finishing) { int index = indexOfTokenLocked(ret); if (index >

= 0) {finishActivityLocked (ret, index, Activity.RESULT_CANCELED, null, "clear");} return null;}} return ret;}} return null;}. } at this point, I believe you have a deeper understanding of "what is the role of Activity's singleTask". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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