本系列博客将详细阐述Activity的启动流程,这些博客基于Cm 10.1源码研究。

前面两篇博客介绍了Activity的详细启动流程,提到ActivityStack类的startActivityUncheckedLocked方法负责调度ActivityRecord和Task,并且调度算法非常复杂,需结合实际场景分析调度算法。本篇博客将介绍startActivityUncheckedLocked方法的具体实现,本结合实际场景分析调度算法。

startActivityUncheckedLocked方法的具体实现

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
  46. 46
  47. 47
  48. 48
  49. 49
  50. 50
  51. 51
  52. 52
  53. 53
  54. 54
  55. 55
  56. 56
  57. 57
  58. 58
  59. 59
  60. 60
  61. 61
  62. 62
  63. 63
  64. 64
  65. 65
  66. 66
  67. 67
  68. 68
  69. 69
  70. 70
  71. 71
  72. 72
  73. 73
  74. 74
  75. 75
  76. 76
  77. 77
  78. 78
  79. 79
  80. 80
  81. 81
  82. 82
  83. 83
  84. 84
  85. 85
  86. 86
  87. 87
  88. 88
  89. 89
  90. 90
  91. 91
  92. 92
  93. 93
  94. 94
  95. 95
  96. 96
  97. 97
  98. 98
  99. 99
  100. 100
  101. 101
  102. 102
  103. 103
  104. 104
  105. 105
  106. 106
  107. 107
  108. 108
  109. 109
  110. 110
  111. 111
  112. 112
  113. 113
  114. 114
  115. 115
  116. 116
  117. 117
  118. 118
  119. 119
  120. 120
  121. 121
  122. 122
  123. 123
  124. 124
  125. 125
  126. 126
  127. 127
  128. 128
  129. 129
  130. 130
  131. 131
  132. 132
  133. 133
  134. 134
  135. 135
  136. 136
  137. 137
  138. 138
  139. 139
  140. 140
  141. 141
  142. 142
  143. 143
  144. 144
  145. 145
  146. 146
  147. 147
  148. 148
  149. 149
  150. 150
  151. 151
  152. 152
  153. 153
  154. 154
  155. 155
  156. 156
  157. 157
  158. 158
  159. 159
  160. 160
  161. 161
  162. 162
  163. 163
  164. 164
  165. 165
  166. 166
  167. 167
  168. 168
  169. 169
  170. 170
  171. 171
  172. 172
  173. 173
  174. 174
  175. 175
  176. 176
  177. 177
  178. 178
  179. 179
  180. 180
  181. 181
  182. 182
  183. 183
  184. 184
  185. 185
  186. 186
  187. 187
  188. 188
  189. 189
  190. 190
  191. 191
  192. 192
  193. 193
  194. 194
  195. 195
  196. 196
  197. 197
  198. 198
  199. 199
  200. 200
  201. 201
  202. 202
  203. 203
  204. 204
  205. 205
  206. 206
  207. 207
  208. 208
  209. 209
  210. 210
  211. 211
  212. 212
  213. 213
  214. 214
  215. 215
  216. 216
  217. 217
  218. 218
  219. 219
  220. 220
  221. 221
  222. 222
  223. 223
  224. 224
  225. 225
  226. 226
  227. 227
  228. 228
  229. 229
  230. 230
  231. 231
  232. 232
  233. 233
  234. 234
  235. 235
  236. 236
  237. 237
  238. 238
  239. 239
  240. 240
  241. 241
  242. 242
  243. 243
  244. 244
  245. 245
  246. 246
  247. 247
  248. 248
  249. 249
  250. 250
  251. 251
  252. 252
  253. 253
  254. 254
  255. 255
  256. 256
  257. 257
  258. 258
  259. 259
  260. 260
  261. 261
  262. 262
  263. 263
  264. 264
  265. 265
  266. 266
  267. 267
  268. 268
  269. 269
  270. 270
  271. 271
  272. 272
  273. 273
  274. 274
  275. 275
  276. 276
  277. 277
  278. 278
  279. 279
  280. 280
  281. 281
  282. 282
  283. 283
  284. 284
  285. 285
  286. 286
  287. 287
  288. 288
  289. 289
  290. 290
  291. 291
  292. 292
  293. 293
  294. 294
  295. 295
  296. 296
  297. 297
  298. 298
  299. 299
  300. 300
  301. 301
  302. 302
  303. 303
  304. 304
  305. 305
  306. 306
  307. 307
  308. 308
  309. 309
  310. 310
  311. 311
  312. 312
  313. 313
  314. 314
  315. 315
  316. 316
  317. 317
  318. 318
  319. 319
  320. 320
  321. 321
  322. 322
  323. 323
  324. 324
  325. 325
  326. 326
  327. 327
  328. 328
  329. 329
  330. 330
  331. 331
  332. 332
  333. 333
  334. 334
  335. 335
  336. 336
  337. 337
  338. 338
  339. 339
  340. 340
  341. 341
  342. 342
  343. 343
  344. 344
  345. 345
  346. 346
  347. 347
  348. 348
  349. 349
  350. 350
  351. 351
  352. 352
  353. 353
  354. 354
  355. 355
  356. 356
  357. 357
  358. 358
  359. 359
  360. 360
  361. 361
  362. 362
  363. 363
  364. 364
  365. 365
  366. 366
  367. 367
  368. 368
  369. 369
  370. 370
  1. final int startActivityUncheckedLocked(ActivityRecord r,
  2. ActivityRecord sourceRecord, int startFlags, boolean doResume,
  3. Bundle options) {
  4. //...
  5. //如果从Launcher程序启动应用,launchFlags为
  6. //FLAG_ACTIVITY_NEW_TASK|FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
  7. //否则一般情况下launcheFlags为0,除非启动Activity时设置了特殊的flag
  8. int launchFlags = intent.getFlags();
  9. //启动Activity时默认不会设置FLAG_ACTIVITY_PREVIOUS_IS_TOP
  10. //故此notTop默认情况下会是null
  11. ActivityRecord notTop = (launchFlags&Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP)
  12. != 0 ? r : null;
  13. //默认情况下startFlags不会设置START_FLAG_ONLY_IF_NEEDED
  14. // If the onlyIfNeeded flag is set, then we can do this if the activity
  15. // being launched is the same as the one making the call... or, as
  16. // a special case, if we do not know the caller then we count the
  17. // current top activity as the caller.
  18. if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
  19. //...默认情况下这里的代码不会执行
  20. }
  21. //根据被启动的Activity和sourceRecord设置标志
  22. //launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK
  23. //如果从通知栏启动应用 sourceRecord == null
  24. if (sourceRecord == null) {
  25. // This activity is not being started from another... in this
  26. // case we -always- start a new task.
  27. if ((launchFlags&Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
  28. Slog.w(TAG, "startActivity called from non-Activity context;"
  29. +"forcing Intent.FLAG_ACTIVITY_NEW_TASK for: "
  30. + intent);
  31. launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
  32. }
  33. } else if (sourceRecord.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
  34. // The original activity who is starting us is running as a single
  35. // instance... this new activity it is starting must go on its
  36. // own task.
  37. launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
  38. } else if (r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE
  39. || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK) {
  40. // The activity being started is a single instance... it always
  41. // gets launched into its own task.
  42. launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
  43. }
  44.  
  45. //一般情况下r.resultTo 不为null,它是启动该Activity的Activity,
  46. //如果从通知栏启动Activity 则r.result为null
  47. if (r.resultTo != null && (launchFlags&Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
  48. //...
  49. r.resultTo = null;
  50. }
  51. //addingToTask 如果为true表示正在添加至某个task,
  52. // 后续需要将r添加至sourceRecord所在的task
  53. boolean addingToTask = false;
  54. //movedHome表示是否移动home task
  55. boolean movedHome = false;
  56. //reuseTask 如果不为null,则表示已存在task,会重用这个task,
  57. // 但是这个Task里的所有Activity会被清除掉,
  58. // 需要将r加入这个task
  59. TaskRecord reuseTask = null;
  60. if (((launchFlags&Intent.FLAG_ACTIVITY_NEW_TASK) != 0 &&
  61. (launchFlags&Intent.FLAG_ACTIVITY_MULTIPLE_TASK) == 0)
  62. || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK
  63. || r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
  64. //从通知栏启动时r.resultTo == null
  65. //如果launchFlags设置了FLAG_ACTIVITY_NEW_TASK,r.resultTo也会为null
  66. if (r.resultTo == null) {
  67. //查找ActivityRecord栈,看要启动的activity是否已有相关task,
  68. //如果已经有相关task,则不需要创建新的task,可以使用已有的task
  69. //如果要启动的activity的启动模式是LAUNCH_SINGLE_INSTANCE,
  70. //则使用快速查找方法findTaskLocked,否则使用慢速查找方法findActivityLocked
  71. //因为如果启动模式是LAUNCH_SINGLE_INSTANCE,则这个activity只会在一个单独的Task里
  72. //故此查找时,可以以task为单位进行查找和比较,这样比较快
  73. //查找得到的结果taskTop是相关task的栈顶的ActivityRecord
  74. // See if there is a task to bring to the front. If this is
  75. // a SINGLE_INSTANCE activity, there can be one and only one
  76. // instance of it in the history, and it is always in its own
  77. // unique task, so we do a special search.
  78. ActivityRecord taskTop = r.launchMode != ActivityInfo.LAUNCH_SINGLE_INSTANCE
  79. ? findTaskLocked(intent, r.info)
  80. : findActivityLocked(intent, r.info);
  81. //找到了相关task
  82. if (taskTop != null) {
  83. //重设task的intent
  84. if (taskTop.task.intent == null) {
  85. // This task was started because of movement of
  86. // the activity based on affinity... now that we
  87. // are actually launching it, we can assign the
  88. // base intent.
  89. taskTop.task.setIntent(intent, r.info);
  90. }
  91. //如果目标task不在栈顶,
  92. //则先将Home task移动到栈顶(实际上只有当启动Activity设置的Flag同时设置了
  93. //FLAG_ACTIVITY_TASK_ON_HOME和FLAG_ACTIVITY_NEW_TASK才会移动home task,
  94. //否则不会移动home task),
  95. //然后再将目标task移动到栈顶
  96. // If the target task is not in the front, then we need
  97. // to bring it to the front... except... well, with
  98. // SINGLE_TASK_LAUNCH it's not entirely clear. We'd like
  99. // to have the same behavior as if a new instance was
  100. // being started, which means not bringing it to the front
  101. // if the caller is not itself in the front.
  102. ActivityRecord curTop = topRunningNonDelayedActivityLocked(notTop);
  103. if (curTop != null && curTop.task != taskTop.task) {
  104. r.intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
  105. boolean callerAtFront = sourceRecord == null
  106. || curTop.task == sourceRecord.task;
  107. if (callerAtFront) {
  108. // We really do want to push this one into the
  109. // user's face, right now.
  110. movedHome = true;
  111. moveHomeToFrontFromLaunchLocked(launchFlags);
  112. moveTaskToFrontLocked(taskTop.task, r, options);
  113. options = null;
  114. }
  115. }
  116. //如果launchFlags设置了FLAG_ACTIVITY_RESET_TASK_IF_NEEDED,则会重置task
  117. //从Launcher应用程序启动应用会设置FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
  118. // If the caller has requested that the target task be
  119. // reset, then do so.
  120. if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
  121. taskTop = resetTaskIfNeededLocked(taskTop, r);
  122. }
  123. //... 一般情况下startFlags 不会设置 START_FLAG_ONLY_IF_NEEDED
  124. if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
  125. //...
  126. }
  127. // ==================================
  128. //默认情况下不会设置 Intent.FLAG_ACTIVITY_CLEAR_TASK
  129. if ((launchFlags &
  130. (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK))
  131. == (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK)) {
  132. // The caller has requested to completely replace any
  133. // existing task with its new activity. Well that should
  134. // not be too hard...
  135. reuseTask = taskTop.task;
  136. performClearTaskLocked(taskTop.task.taskId);
  137. reuseTask.setIntent(r.intent, r.info);
  138. } else if ((launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0
  139. || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK
  140. || r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
  141. //默认情况下launchFlags不会设置FLAG_ACTIVITY_CLEAR_TOP
  142. //但是如果被启动的activity的启动模式是singleTask或者singleInstance,
  143. //也会进入该分支
  144. // In this situation we want to remove all activities
  145. // from the task up to the one being started. In most
  146. // cases this means we are resetting the task to its
  147. // initial state.
  148. //清除r所在的task 在r之上的所有activity,
  149. //该task里r和在r下的activity不会被清除
  150. ActivityRecord top = performClearTaskLocked(
  151. taskTop.task.taskId, r, launchFlags);
  152. if (top != null) {
  153. if (top.frontOfTask) {
  154. // Activity aliases may mean we use different
  155. // intents for the top activity, so make sure
  156. // the task now has the identity of the new
  157. // intent.
  158. top.task.setIntent(r.intent, r.info);
  159. }
  160. logStartActivity(EventLogTags.AM_NEW_INTENT, r, top.task);
  161. top.deliverNewIntentLocked(callingUid, r.intent);
  162. } else {
  163. // A special case: we need to
  164. // start the activity because it is not currently
  165. // running, and the caller has asked to clear the
  166. // current task to have this activity at the top.
  167. addingToTask = true;
  168. // Now pretend like this activity is being started
  169. // by the top of its task, so it is put in the
  170. // right place.
  171. sourceRecord = taskTop;
  172. }
  173. } else if (r.realActivity.equals(taskTop.task.realActivity)) {
  174. // In this case the top activity on the task is the
  175. // same as the one being launched, so we take that
  176. // as a request to bring the task to the foreground.
  177. // If the top activity in the task is the root
  178. // activity, deliver this new intent to it if it
  179. // desires.
  180. if (((launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
  181. || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP)
  182. && taskTop.realActivity.equals(r.realActivity)) {
  183. logStartActivity(EventLogTags.AM_NEW_INTENT, r, taskTop.task);
  184. if (taskTop.frontOfTask) {
  185. taskTop.task.setIntent(r.intent, r.info);
  186. }
  187. taskTop.deliverNewIntentLocked(callingUid, r.intent);
  188. } else if (!r.intent.filterEquals(taskTop.task.intent)) {
  189. // In this case we are launching the root activity
  190. // of the task, but with a different intent. We
  191. // should start a new instance on top.
  192. addingToTask = true;
  193. sourceRecord = taskTop;
  194. }
  195. } else if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) == 0) {
  196. // In this case an activity is being launched in to an
  197. // existing task, without resetting that task. This
  198. // is typically the situation of launching an activity
  199. // from a notification or shortcut. We want to place
  200. // the new activity on top of the current task.
  201. addingToTask = true;
  202. sourceRecord = taskTop;
  203. } else if (!taskTop.task.rootWasReset) {
  204. //进入该分支的情况比较少
  205. // In this case we are launching in to an existing task
  206. // that has not yet been started from its front door.
  207. // The current task has been brought to the front.
  208. // Ideally, we'd probably like to place this new task
  209. // at the bottom of its stack, but that's a little hard
  210. // to do with the current organization of the code so
  211. // for now we'll just drop it.
  212. taskTop.task.setIntent(r.intent, r.info);
  213. }
  214. // ================================== end
  215. //如果没有正在添加至某个Task, 并且不用加入一个已清除所有Activity的Task
  216. //此时只需要显示栈顶Activity即可
  217. if (!addingToTask && reuseTask == null) {
  218. // We didn't do anything... but it was needed (a.k.a., client
  219. // don't use that intent!) And for paranoia, make
  220. // sure we have correctly resumed the top activity.
  221. if (doResume) {
  222. resumeTopActivityLocked(null, options);
  223. } else {
  224. ActivityOptions.abort(options);
  225. }
  226. return ActivityManager.START_TASK_TO_FRONT;
  227. }
  228. }
  229. }
  230. }
  231. //...
  232. if (r.packageName != null) {
  233. // If the activity being launched is the same as the one currently
  234. // at the top, then we need to check if it should only be launched
  235. // once.
  236. ActivityRecord top = topRunningNonDelayedActivityLocked(notTop);
  237. if (top != null && r.resultTo == null) {
  238. if (top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
  239. if (top.app != null && top.app.thread != null) {
  240. if ((launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
  241. || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP
  242. || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK) {
  243. //singleTop启动模式或者singleTask启动模式,
  244. //并且task栈顶的activity是要启动的activity,则先显示Activity
  245. //然后调用该Activity的onNewIntent方法
  246. logStartActivity(EventLogTags.AM_NEW_INTENT, top, top.task);
  247. // For paranoia, make sure we have correctly
  248. // resumed the top activity.
  249. //先显示Activity
  250. if (doResume) {
  251. resumeTopActivityLocked(null);
  252. }
  253. ActivityOptions.abort(options);
  254. if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
  255. // We don't need to start a new activity, and
  256. // the client said not to do anything if that
  257. // is the case, so this is it!
  258. return ActivityManager.START_RETURN_INTENT_TO_CALLER;
  259. }
  260. //然后调用已显示activity的onNewIntent方法
  261. top.deliverNewIntentLocked(callingUid, r.intent);
  262. return ActivityManager.START_DELIVERED_TO_TOP;
  263. }
  264. }
  265. }
  266. }
  267.  
  268. } else {
  269. if (r.resultTo != null) {
  270. sendActivityResultLocked(-1,
  271. r.resultTo, r.resultWho, r.requestCode,
  272. Activity.RESULT_CANCELED, null);
  273. }
  274. ActivityOptions.abort(options);
  275. return ActivityManager.START_CLASS_NOT_FOUND;
  276. }
  277. boolean newTask = false;
  278. boolean keepCurTransition = false;
  279. // Should this be considered a new task?
  280. if (r.resultTo == null && !addingToTask
  281. && (launchFlags&Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
  282. if (reuseTask == null) {
  283. //创建新的task
  284. // todo: should do better management of integers.
  285. mService.mCurTask++;
  286. if (mService.mCurTask <= 0) {
  287. mService.mCurTask = 1;
  288. }
  289. r.setTask(new TaskRecord(mService.mCurTask, r.info, intent), null, true);
  290. if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r
  291. + " in new task " + r.task);
  292. } else {
  293. //重复利用先前的task,该task里的所有acitivity已经被清空
  294. r.setTask(reuseTask, reuseTask, true);
  295. }
  296. newTask = true;
  297. if (!movedHome) {
  298. moveHomeToFrontFromLaunchLocked(launchFlags);
  299. }
  300. } else if (sourceRecord != null) {
  301. if (!addingToTask &&
  302. (launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
  303. // In this case, we are adding the activity to an existing
  304. // task, but the caller has asked to clear that task if the
  305. // activity is already running.
  306. //清除r所在task在r之上的所有task,如果r不在task里,则返回的top为null
  307. ActivityRecord top = performClearTaskLocked(
  308. sourceRecord.task.taskId, r, launchFlags);
  309. keepCurTransition = true;
  310. if (top != null) {
  311. logStartActivity(EventLogTags.AM_NEW_INTENT, r, top.task);
  312. //先调用onNewIntent方法 然后再显示
  313. top.deliverNewIntentLocked(callingUid, r.intent);
  314. // For paranoia, make sure we have correctly
  315. // resumed the top activity.
  316. if (doResume) {
  317. resumeTopActivityLocked(null);
  318. }
  319. ActivityOptions.abort(options);
  320. return ActivityManager.START_DELIVERED_TO_TOP;
  321. }
  322. } else if (!addingToTask &&
  323. (launchFlags&Intent.FLAG_ACTIVITY_REORDER_TO_FRONT) != 0) {
  324. //将栈里已有的activity移到栈顶
  325. // In this case, we are launching an activity in our own task
  326. // that may already be running somewhere in the history, and
  327. // we want to shuffle it to the front of the stack if so.
  328. int where = findActivityInHistoryLocked(r, sourceRecord.task.taskId);
  329. if (where >= 0) {
  330. ActivityRecord top = moveActivityToFrontLocked(where);
  331. logStartActivity(EventLogTags.AM_NEW_INTENT, r, top.task);
  332. top.updateOptionsLocked(options);
  333. top.deliverNewIntentLocked(callingUid, r.intent);
  334. if (doResume) {
  335. resumeTopActivityLocked(null);
  336. }
  337. return ActivityManager.START_DELIVERED_TO_TOP;
  338. }
  339. }
  340. // An existing activity is starting this new activity, so we want
  341. // to keep the new one in the same task as the one that is starting
  342. // it.
  343. //同一个应用程序里的Activity A和Activity B,A可跳转至B,没有设置taskAffinity
  344. //B的启动模式为singleTask,从A跳转至B时,B和A会在同一个task里
  345. //该情况下会执行到这里的代码,将B的task设置为和A一样的task
  346. r.setTask(sourceRecord.task, sourceRecord.thumbHolder, false);
  347. if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r
  348. + " in existing task " + r.task);
  349. } else {
  350. // This not being started from an existing activity, and not part
  351. // of a new task... just put it in the top task, though these days
  352. // this case should never happen.
  353. final int N = mHistory.size();
  354. ActivityRecord prev =
  355. N > 0 ? mHistory.get(N-1) : null;
  356. r.setTask(prev != null
  357. ? prev.task
  358. : new TaskRecord(mService.mCurTask, r.info, intent), null, true);
  359. if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r
  360. + " in new guessed " + r.task);
  361. }
  362. mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
  363. intent, r.getUriPermissionsLocked());
  364. if (newTask) {
  365. EventLog.writeEvent(EventLogTags.AM_CREATE_TASK, r.userId, r.task.taskId);
  366. }
  367. logStartActivity(EventLogTags.AM_CREATE_ACTIVITY, r, r.task);
  368. startActivityLocked(r, newTask, doResume, keepCurTransition, options);
  369. return ActivityManager.START_SUCCESS;
  370. }

实际场景分析

实际场景1:

应用内有两个Activity,A和B,A为第应用入口Activity,从A可跳转至B,A和B的启动模式都为standard

1)从Launcher程序第1次启动应用时的任务调度情况:

任务调度时会创建新task并将新的ActivityRecord加入这个新的task

2)然后跳转至应用内Activity时的任务调度情况:

任务调度时会将新的ActivityRecord加入已有的task

3)然后按Home键,再打开应用程序时的调度情况:

任务调度时会先找到已有的相关task,并显示栈顶的Activity

1)从Launcher程序第1次启动应用时

会创建新task并将新的ActivityRecord加入这个新的task,任务调度执行如下所示:

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
  46. 46
  47. 47
  48. 48
  49. 49
  50. 50
  51. 51
  52. 52
  53. 53
  54. 54
  55. 55
  56. 56
  57. 57
  58. 58
  59. 59
  60. 60
  61. 61
  62. 62
  63. 63
  64. 64
  65. 65
  66. 66
  67. 67
  68. 68
  69. 69
  70. 70
  71. 71
  72. 72
  73. 73
  74. 74
  75. 75
  76. 76
  77. 77
  78. 78
  79. 79
  80. 80
  81. 81
  82. 82
  83. 83
  84. 84
  85. 85
  86. 86
  87. 87
  88. 88
  89. 89
  90. 90
  91. 91
  92. 92
  93. 93
  94. 94
  95. 95
  96. 96
  97. 97
  98. 98
  1. final int startActivityUncheckedLocked(ActivityRecord r,
  2. ActivityRecord sourceRecord, int startFlags, boolean doResume,
  3. Bundle options) {
  4. //...
  5. //launchFlags为FLAG_ACTIVITY_NEW_TASK|FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
  6. int launchFlags = intent.getFlags();
  7. //...
  8. //没设置FLAG_ACTIVITY_PREVIOUS_IS_TOP,故此notTop为null
  9. ActivityRecord notTop = (launchFlags&Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP)
  10. != 0 ? r : null;
  11. //startFlags未设置ActivityManager.START_FLAG_ONLY_IF_NEEDED
  12. //...
  13. //sourceRecord为Launcher应用的Activity launcher应用activity的启动模式为singleTask
  14. // 故此下面的3个条件分支的内容都不会执行
  15. if (sourceRecord == null) {
  16. //...
  17. } else if (sourceRecord.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
  18. //...
  19. } else if (r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE
  20. || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK) {
  21. //...
  22. }
  23. //...
  24. //r.resultTo不为null, launchFlags设置了FLAG_ACTIVITY_NEW_TASK,需要将r.resultTo置为null
  25. if (r.resultTo != null && (launchFlags&Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
  26. //...
  27. r.resultTo = null;
  28. }
  29. boolean addingToTask = false;
  30. boolean movedHome = false;
  31. TaskRecord reuseTask = null;
  32. //因为launchFlags为FLAG_ACTIVITY_NEW_TASK|FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
  33. //故此下面的条件会满足, 也就是说只要从Launcher程序启动应用,下面这个条件肯定会满足
  34. if (((launchFlags&Intent.FLAG_ACTIVITY_NEW_TASK) != 0 &&
  35. (launchFlags&Intent.FLAG_ACTIVITY_MULTIPLE_TASK) == 0)
  36. || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK
  37. || r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
  38. //...
  39. if (r.resultTo == null) {
  40. //因为应用被第一次启动,故此找不到相关task,taskTop则为null
  41. ActivityRecord taskTop = r.launchMode != ActivityInfo.LAUNCH_SINGLE_INSTANCE
  42. ? findTaskLocked(intent, r.info)
  43. : findActivityLocked(intent, r.info);
  44. if (taskTop != null) {
  45. //... 这里面的内容不会执行
  46. }
  47. }
  48. }
  49. //...
  50. //r.packageName != null
  51. if (r.packageName != null) {
  52. //如果被启动的Activity正好是栈顶的Activity,
  53. //并且被启动的Activity启动模式是singleTop或者singleTask,
  54. //则不用将新的ActivityRecord加入到栈里
  55. //top Activity为Launcher应用的Activity
  56. ActivityRecord top = topRunningNonDelayedActivityLocked(notTop);
  57. if (top != null && r.resultTo == null) {
  58. //top.realActivity.equals(r.realActivity)不满足
  59. if (top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
  60. //... 这里的代码不会被执行
  61. }
  62. }
  63.  
  64. } else {
  65. //...
  66. }
  67. boolean newTask = false;
  68. boolean keepCurTransition = false;
  69. // 此时 r.resultTo为null addingToTask为false launchFlags设置了FLAG_ACTIVITY_NEW_TASK
  70. if (r.resultTo == null && !addingToTask
  71. && (launchFlags&Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
  72. if (reuseTask == null) {
  73. // todo: should do better management of integers.
  74. mService.mCurTask++;
  75. if (mService.mCurTask <= 0) {
  76. mService.mCurTask = 1;
  77. }
  78. //创建新task
  79. r.setTask(new TaskRecord(mService.mCurTask, r.info, intent), null, true);
  80. if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r
  81. + " in new task " + r.task);
  82. } else {
  83. //...这里的代码会执行
  84. }
  85. newTask = true;
  86. if (!movedHome) {
  87. moveHomeToFrontFromLaunchLocked(launchFlags);
  88. }
  89.  
  90. } else if (sourceRecord != null) {
  91. //... 这里的代码不会被执行
  92. } else {
  93. //...这里的代码不会被执行
  94. }
  95. //...
  96. startActivityLocked(r, newTask, doResume, keepCurTransition, options);
  97. return ActivityManager.START_SUCCESS;
  98. }

2)跳转至应用内Activity时

会将新的ActivityRecord加入已有的task,任务调度执行如下所示:

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
  46. 46
  47. 47
  48. 48
  49. 49
  50. 50
  51. 51
  52. 52
  53. 53
  54. 54
  55. 55
  56. 56
  57. 57
  58. 58
  59. 59
  60. 60
  61. 61
  62. 62
  63. 63
  64. 64
  65. 65
  66. 66
  67. 67
  68. 68
  69. 69
  1. final int startActivityUncheckedLocked(ActivityRecord r,
  2. ActivityRecord sourceRecord, int startFlags, boolean doResume,
  3. Bundle options) {
  4. //此时launchFlags为0
  5. int launchFlags = intent.getFlags();
  6. //notTop为null
  7. ActivityRecord notTop = (launchFlags&Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP)
  8. != 0 ? r : null;
  9. //startFlags未设置ActivityManager.START_FLAG_ONLY_IF_NEEDED
  10. //...
  11. if (sourceRecord == null) {
  12. //...这里的代码不会被执行
  13. } else if (sourceRecord.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
  14. //...这里的代码不会被执行
  15. } else if (r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE
  16. || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK) {
  17. //...这里的代码不会被执行
  18. }
  19. //r.resultTo != null 但是launchFlags未设置FLAG_ACTIVITY_NEW_TASK
  20. if (r.resultTo != null && (launchFlags&Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
  21. //... 这里的代码不执行
  22. }
  23. boolean addingToTask = false;
  24. boolean movedHome = false;
  25. TaskRecord reuseTask = null;
  26. //launchFlags为0 r的启动模式为standard 故此下面的逻辑都不会执行
  27. if (((launchFlags&Intent.FLAG_ACTIVITY_NEW_TASK) != 0 &&
  28. (launchFlags&Intent.FLAG_ACTIVITY_MULTIPLE_TASK) == 0)
  29. || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK
  30. || r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
  31. //... 这里的代码不执行
  32. }
  33. //...
  34. if (r.packageName != null) {
  35. //top 是ActivityA 的ActivityRecord,
  36. //但是被启动的Activity和top不是同一个Activity
  37. ActivityRecord top = topRunningNonDelayedActivityLocked(notTop);
  38. if (top != null && r.resultTo == null) {
  39. if (top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
  40. //...这里的代码不执行
  41. }
  42. }
  43.  
  44. } else {
  45. //...这里的代码不执行
  46. }
  47. boolean newTask = false;
  48. boolean keepCurTransition = false;
  49. //此时 r.resultTo !=null sourceRecord != null addingToTask=false
  50. if (r.resultTo == null && !addingToTask
  51. && (launchFlags&Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
  52. //...这里的代码不执行
  53. } else if (sourceRecord != null) {
  54. if (!addingToTask &&
  55. (launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
  56. //... 这里的代码不执行
  57. } else if (!addingToTask &&
  58. (launchFlags&Intent.FLAG_ACTIVITY_REORDER_TO_FRONT) != 0) {
  59. //... 这里的代码不执行
  60. }
  61. //添加到现有的task
  62. r.setTask(sourceRecord.task, sourceRecord.thumbHolder, false);
  63. //...
  64. } else {
  65. //... 这里的代码不执行
  66. }
  67. //...
  68. return ActivityManager.START_SUCCESS;
  69. }

3)然后按Home键,再打开应用程序

此时会先找到已有的相关task,并显示栈顶的Activity,任务调度执行如下所示:

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
  46. 46
  47. 47
  48. 48
  49. 49
  50. 50
  51. 51
  52. 52
  53. 53
  54. 54
  55. 55
  56. 56
  57. 57
  58. 58
  59. 59
  60. 60
  61. 61
  62. 62
  63. 63
  64. 64
  65. 65
  66. 66
  67. 67
  68. 68
  69. 69
  70. 70
  71. 71
  72. 72
  73. 73
  74. 74
  75. 75
  76. 76
  77. 77
  78. 78
  79. 79
  80. 80
  81. 81
  82. 82
  83. 83
  84. 84
  85. 85
  86. 86
  87. 87
  88. 88
  89. 89
  90. 90
  91. 91
  92. 92
  93. 93
  94. 94
  95. 95
  96. 96
  97. 97
  98. 98
  99. 99
  100. 100
  101. 101
  102. 102
  103. 103
  1. final int startActivityUncheckedLocked(ActivityRecord r,
  2. ActivityRecord sourceRecord, int startFlags, boolean doResume,
  3. Bundle options) {
  4. //...
  5. //launchFlags为FLAG_ACTIVITY_NEW_TASK|FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
  6. int launchFlags = intent.getFlags();
  7. //notTop为null
  8. ActivityRecord notTop = (launchFlags&Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP)
  9. != 0 ? r : null;
  10. //startFlags未设置ActivityManager.START_FLAG_ONLY_IF_NEEDED
  11. //...
  12. if (sourceRecord == null) {
  13. //...这里的代码不会被执行
  14. } else if (sourceRecord.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
  15. //...这里的代码不会被执行
  16. } else if (r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE
  17. || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK) {
  18. //...这里的代码不会被执行
  19. }
  20. //此时 r.resultTo != null launchFlags设置了FLAG_ACTIVITY_NEW_TASK
  21. if (r.resultTo != null && (launchFlags&Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
  22. //...
  23. r.resultTo = null;
  24. }
  25. boolean addingToTask = false;
  26. boolean movedHome = false;
  27. TaskRecord reuseTask = null;
  28. //此时launchFlags设置了FLAG_ACTIVITY_NEW_TASK|FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
  29. if (((launchFlags&Intent.FLAG_ACTIVITY_NEW_TASK) != 0 &&
  30. (launchFlags&Intent.FLAG_ACTIVITY_MULTIPLE_TASK) == 0)
  31. || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK
  32. || r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
  33. //此时 r.resultTo == null
  34. if (r.resultTo == null) {
  35. //此时已有相关task,并且task 栈的栈顶是Activity B的ActivityRecord
  36. //故此taskTop为Activity B的ActivityRecord
  37. ActivityRecord taskTop = r.launchMode != ActivityInfo.LAUNCH_SINGLE_INSTANCE
  38. ? findTaskLocked(intent, r.info)
  39. : findActivityLocked(intent, r.info);
  40. if (taskTop != null) {
  41. //...
  42. // 此时curTop是Launcher应用的Activity的ActivityRecord
  43. ActivityRecord curTop = topRunningNonDelayedActivityLocked(notTop);
  44. if (curTop != null && curTop.task != taskTop.task) {
  45. r.intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
  46. //此时Launcher应用的task在栈顶,故此callerAtFront为true,
  47. //此时会把被启动的应用的task移至栈顶
  48. boolean callerAtFront = sourceRecord == null
  49. || curTop.task == sourceRecord.task;
  50. if (callerAtFront) {
  51. // We really do want to push this one into the
  52. // user's face, right now.
  53. movedHome = true;
  54. moveHomeToFrontFromLaunchLocked(launchFlags);
  55. moveTaskToFrontLocked(taskTop.task, r, options);
  56. options = null;
  57. }
  58. }
  59. //此时launchFlags设置了FLAG_ACTIVITY_NEW_TASK|FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
  60. //此时需要重置task 重置完后 taskTop为ActivityB的ActivityRecord
  61. if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
  62. taskTop = resetTaskIfNeededLocked(taskTop, r);
  63. }
  64. //startFlags为0
  65. if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
  66. //... 这些代码都不会被执行
  67. }
  68. //根据launchFlags和被启动的activity的信息 设置resueTask addingTask变量的值
  69. //没设置 Intent.FLAG_ACTIVITY_CLEAR_TASK
  70. if ((launchFlags &
  71. (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK))
  72. == (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK)) {
  73. //... 这些代码都不会被执行
  74. } else if ((launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0
  75. || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK
  76. || r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
  77. //... 这些代码都不会被执行
  78. } else if (r.realActivity.equals(taskTop.task.realActivity)) {
  79. //... 这些代码都不会被执行
  80. } else if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) == 0) {
  81. //因为从Launcher程序启动时launchFlags设置了FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
  82. //所以不会进入该分支
  83. //... 这些代码都不会被执行
  84. } else if (!taskTop.task.rootWasReset) {
  85. //... 这些代码都不会被执行
  86. }
  87. //此时addingToTask为false,reuseTask为null,故此显示栈顶Actvity即可
  88. if (!addingToTask && reuseTask == null) {
  89. // We didn't do anything... but it was needed (a.k.a., client
  90. // don't use that intent!) And for paranoia, make
  91. // sure we have correctly resumed the top activity.
  92. if (doResume) {
  93. resumeTopActivityLocked(null, options);
  94. } else {
  95. ActivityOptions.abort(options);
  96. }
  97. return ActivityManager.START_TASK_TO_FRONT;
  98. }
  99. }
  100. }
  101. }
  102. //... 以下代码都不会被执行
  103. }

实际场景2:

应用内有两个Activity,A和B,A为第应用入口Activity,从A可跳转至B,A的启动模式都为standard,B的启动模式为singleTop

此时已从Launchenr程序打开应用,启动了Actvity A,再从A跳转至B,此时的任务调度情况:

此时不会创建新的Task,而是将B的ActivityRecord加入到A所在的task里

任务调度执行如下所示:

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
  46. 46
  47. 47
  48. 48
  49. 49
  50. 50
  51. 51
  52. 52
  53. 53
  54. 54
  55. 55
  56. 56
  57. 57
  58. 58
  59. 59
  60. 60
  61. 61
  62. 62
  63. 63
  64. 64
  65. 65
  66. 66
  67. 67
  68. 68
  69. 69
  70. 70
  71. 71
  72. 72
  73. 73
  74. 74
  75. 75
  76. 76
  77. 77
  78. 78
  79. 79
  80. 80
  81. 81
  82. 82
  83. 83
  84. 84
  85. 85
  86. 86
  87. 87
  88. 88
  89. 89
  90. 90
  91. 91
  92. 92
  93. 93
  94. 94
  95. 95
  96. 96
  97. 97
  98. 98
  99. 99
  100. 100
  101. 101
  102. 102
  103. 103
  104. 104
  105. 105
  106. 106
  107. 107
  108. 108
  109. 109
  110. 110
  111. 111
  112. 112
  113. 113
  114. 114
  115. 115
  116. 116
  117. 117
  118. 118
  119. 119
  120. 120
  121. 121
  122. 122
  123. 123
  124. 124
  125. 125
  126. 126
  127. 127
  128. 128
  129. 129
  130. 130
  131. 131
  132. 132
  133. 133
  134. 134
  135. 135
  136. 136
  137. 137
  138. 138
  139. 139
  140. 140
  1. final int startActivityUncheckedLocked(ActivityRecord r,
  2. ActivityRecord sourceRecord, int startFlags, boolean doResume,
  3. Bundle options) {
  4. //...
  5. //此时launcheFlags为0
  6. int launchFlags = intent.getFlags();
  7. //notTop为null
  8. ActivityRecord notTop = (launchFlags&Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP)
  9. != 0 ? r : null;
  10. //默认情况下startFlags不会设置START_FLAG_ONLY_IF_NEEDED
  11. if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
  12. //...这里的代码不会执行
  13. }
  14. //r.launchMode = ActivityInfo.LAUNCH_SINGLE_TASK
  15. if (sourceRecord == null) {
  16. //这里的代码不会执行
  17. } else if (sourceRecord.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
  18. //这里的代码不会执行
  19. } else if (r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE
  20. || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK) {
  21. launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
  22. }
  23.  
  24. //此时r.resultTo!=null launchFlags设置了Intent.FLAG_ACTIVITY_NEW_TASK
  25. if (r.resultTo != null && (launchFlags&Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
  26. //...
  27. r.resultTo = null;
  28. }
  29. //addingToTask如果为true表示正在添加至某个task,后续需要将r添加至sourceRecord所在的task
  30. boolean addingToTask = false;
  31. //movedHome表示是否移动home task
  32. boolean movedHome = false;
  33. TaskRecord reuseTask = null;
  34. if (((launchFlags&Intent.FLAG_ACTIVITY_NEW_TASK) != 0 &&
  35. (launchFlags&Intent.FLAG_ACTIVITY_MULTIPLE_TASK) == 0)
  36. || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK
  37. || r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
  38. //此时 r.resultTo = null
  39. if (r.resultTo == null) {
  40. //此时找到的taskTop是Activity A的ActivityRecord,
  41. //因为Actvity B和A的ActivityRecord所在的Task是相关的
  42. ActivityRecord taskTop = r.launchMode != ActivityInfo.LAUNCH_SINGLE_INSTANCE
  43. ? findTaskLocked(intent, r.info)
  44. : findActivityLocked(intent, r.info);
  45. //找到了相关task
  46. if (taskTop != null) {
  47. //重设task的intent
  48. if (taskTop.task.intent == null) {
  49. //...
  50. }
  51. //此时找到的task已在栈顶
  52. ActivityRecord curTop = topRunningNonDelayedActivityLocked(notTop);
  53. if (curTop != null && curTop.task != taskTop.task) {
  54. //... 这里的代码不会执行
  55. }
  56. //launchFlags为0
  57. if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
  58. taskTop = resetTaskIfNeededLocked(taskTop, r);
  59. }
  60. //... 一般情况下startFlags 不会设置 START_FLAG_ONLY_IF_NEEDED
  61. if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
  62. //...
  63. }
  64.  
  65. // ==================== begin
  66. // launchFlags此时为0
  67. if ((launchFlags &
  68. (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK))
  69. == (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK)) {
  70. //...这里的代码不执行
  71. } else if ((launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0
  72. || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK
  73. || r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
  74. // r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK
  75. // 故此会进入该分支
  76. //因为B还从未启动,故此得到的top为null
  77. ActivityRecord top = performClearTaskLocked(
  78. taskTop.task.taskId, r, launchFlags);
  79. if (top != null) {
  80. //...这里的代码不执行
  81. } else {
  82. addingToTask = true;
  83. sourceRecord = taskTop;
  84. }
  85. } else if (r.realActivity.equals(taskTop.task.realActivity)) {
  86. //...这里的代码不执行
  87. } else if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) == 0) {
  88. //...这里的代码不执行
  89. } else if (!taskTop.task.rootWasReset) {
  90. //...这里的代码不执行
  91. }
  92. // ==================== end
  93.  
  94. // 此时 addingToTask为true
  95. if (!addingToTask && reuseTask == null) {
  96. //...这里的代码不执行
  97. }
  98. }
  99. }
  100. }
  101. //...
  102.  
  103. if (r.packageName != null) {
  104. ActivityRecord top = topRunningNonDelayedActivityLocked(notTop);
  105. if (top != null && r.resultTo == null) {
  106. //此时task还没有B的ActivityRecord,故此不会进入下述分支
  107. if (top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
  108. //...这里的代码不执行
  109. }
  110. }
  111.  
  112. } else {
  113. //...这里的代码不执行
  114. }
  115.  
  116. boolean newTask = false;
  117. boolean keepCurTransition = false;
  118.  
  119. // 此时 r.resultTo == null addingToTask为true sourceRecord != null
  120. if (r.resultTo == null && !addingToTask
  121. && (launchFlags&Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
  122. //...这里的代码不执行
  123. } else if (sourceRecord != null) {
  124. if (!addingToTask &&
  125. (launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
  126. //...这里的代码不执行
  127. } else if (!addingToTask &&
  128. (launchFlags&Intent.FLAG_ACTIVITY_REORDER_TO_FRONT) != 0) {
  129. //...这里的代码不执行
  130. }
  131. //将B的ActivityRecord加入A的ActivityRecord所在的Task里
  132. r.setTask(sourceRecord.task, sourceRecord.thumbHolder, false);
  133. //...
  134. } else {
  135. //...这里的代码不执行
  136. }
  137. //...
  138. startActivityLocked(r, newTask, doResume, keepCurTransition, options);
  139. return ActivityManager.START_SUCCESS;
  140. }

总结

从上面的分析可以看出来,Activity和Task的调度算法非常复杂,需结合实际场景才好分析,只有这样才知道是否需要新建Task,还是将新的ActivityRecord加入到已有的Task里,不过我们如果能理解启动模式的一些特点,对理解调度算法会有很大帮助。

大家可以结合下述场景分析调度算法:

1.从通知栏启动Activity:

假设应用有Activity A ,Activity A已启动,

此时发了一个通知,该通知用于启动Activity A,启动Activity A时不加任何特殊flag

点击通知,针对以下情况对任务调度情况进行分析:

1) Activity A的启动模式为standard

2) Activity A的启动模式为singleTop

3) Activity A的启动模式为singleTask

4) Activity A的启动模式为singleInstance

2.跨应用跳转Activity

假设应用app1有一个Activity A,另一个应用app2有一个Activity B

Activity A可跳转至Activity B

因为Activity A和Actiivty B在不同应用,所以Activity的taskffinity必然不同

现在Activity A已启动,跳转至Activity B,

针对以下4种情况分析跳转之后的Activity Task情况

1) Activity B的启动模式为standard

2) Activity B的启动模式为singleTop

3) Activity B的启动模式为singleTask

4) Activity B的启动模式为singleInstance

如果大家对上述场景分析有兴趣的话,可以在评论里一起探讨结果。

深入理解Activity启动流程(四)–Activity Task的调度算法的更多相关文章

  1. 《转》深入理解Activity启动流程(四)–Activity Task的调度算法

    本文原创作者:Cloud Chou. 出处:本文链接 本系列博客将详细阐述Activity的启动流程,这些博客基于Cm 10.1源码研究. 深入理解Activity启动流程(一)--Activity启 ...

  2. 《转》深入理解Activity启动流程(三)–Activity启动的详细流程2

    本文原创作者:Cloud Chou. 出处:本文链接 本系列博客将详细阐述Activity的启动流程,这些博客基于Cm 10.1源码研究. 深入理解Activity启动流程(一)--Activity启 ...

  3. 《转》深入理解Activity启动流程(三)–Activity启动的详细流程1

    本文原创作者:Cloud Chou. 出处:本文链接 本系列博客将详细阐述Activity的启动流程,这些博客基于Cm 10.1源码研究. 深入理解Activity启动流程(一)--Activity启 ...

  4. 《转》深入理解Activity启动流程(二)–Activity启动相关类的类图

    本文原创作者:Cloud Chou. 出处:本文链接 本系列博客将详细阐述Activity的启动流程,这些博客基于Cm 10.1源码研究. 在介绍Activity的详细启动流程之前,先为大家介绍Act ...

  5. 《转》深入理解Activity启动流程(一)–Activity启动的概要流程

    本文原创作者:Cloud Chou. 原文地址:http://www.cloudchou.com/android/post-788.html Android中启动某个Activity,将先启动Acti ...

  6. 深入理解Activity启动流程(三)–Activity启动的详细流程2

    本文原创作者:Cloud Chou. 欢迎转载,请注明出处和本文链接 本系列博客将详细阐述Activity的启动流程,这些博客基于Cm 10.1源码研究. 深入理解Activity启动流程(一)--A ...

  7. 深入理解Activity启动流程(二)–Activity启动相关类的类图

    本文原创作者:Cloud Chou. 欢迎转载,请注明出处和本文链接 本系列博客将详细阐述Activity的启动流程,这些博客基于Cm 10.1源码研究. 在介绍Activity的详细启动流程之前,先 ...

  8. 深入理解Activity启动流程(一)–Activity启动的概要流程

    概述 Android中启动某个Activity,将先启动Activity所在的应用.应用启动时会启动一个以应用包名为进程名的进程,该进程有一个主线程,叫ActivityThread,也叫做UI线程. ...

  9. Android之Activity启动流程详解(基于api28)

    前言 Activity作为Android四大组件之一,他的启动绝对没有那么简单.这里涉及到了系统服务进程,启动过程细节很多,这里我只展示主体流程.activity的启动流程随着版本的更替,代码细节一直 ...

随机推荐

  1. dll和lib关系及使用

    对于dll和lib两者的关系,需要理解的一个概念是编译时和运行时.   lib是编译时的东西,在lib里面包含了方法名和方法所在的dll名字,可以用dumpbin -all XXX.lib查看内容. ...

  2. 通用的高度可扩展的Excel导入实现(附Demo)

    Demo源码 背景 通过程序将excel导入到数据库中是一项非常常见的功能.通常的做法是:先将excel转成DataTable,然后将DataTable转换成List<T>,最终通过Lis ...

  3. 解决debian 9 重启nameserver失效问题

    目录 解决debian 9 重启nameserver失效问题 安装resolvconf 编辑文件 测试 解决debian 9 重启nameserver失效问题 刚安装完debian9,用过之后会发现/ ...

  4. 数据库导入Exel,输入到浏览器

    db.php <?php require dirname(__FILE__)."/dbconfig.php";//引入配置文件 class db{ public $conn= ...

  5. Python hash、xml、configparser、sheve、shutil模块讲解 以及 面向对象初识

    今日内容: 1.hash模块2.xml模块3.configparser模块4.sheve 模块5.shutil模块 知识点一:hash什么是hash: hash是一种算法,该算法接受传入的的内容,经过 ...

  6. google浏览器audits

    功能翻译 audits,审计 Audits help you identify and fix common problems that affect your site'sperformance,a ...

  7. HLG2179 组合(dfs水水更健康)

    组合 Time Limit: 1000 MS Memory Limit: 32768 K Total Submit: 57(38 users) Total Accepted: 43(36 users) ...

  8. CS231n笔记 Lecture 5 Convolutional Neural Networks

    一些ConvNets的应用 Face recognition 输入人脸,推测是谁 Video classfication Recognition 识别身体的部位, 医学图像, 星空, 标志牌, 鲸.. ...

  9. IE7中a标签包含img,点击img,链接失效的bug

    在做列表时,我们经常会这样写: <ul class="works-list"> <li> <a href=""> <d ...

  10. ACM程序设计选修课——1076汇编语言(重定向+模拟)

    1076: 汇编语言 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 34  Solved: 4 [Submit][Status][Web Board] ...