你好,游客 登录 注册 搜索
背景:
阅读新闻

Android应用程序内部启动Activity过程(startActivity)的源代码分析

[日期:2011-08-29] 来源:Linux社区  作者:luoshengyang [字体:大 中 小]
这里,参数intent的标志位Intent.FLAG_ACTIVITY_NEW_TASK没有设置,在配置文件AndriodManifest.xml中,SubActivity也没有配置启动模式launchMode,于是它就默认标准模式,即ActivityInfo.LAUNCH_MULTIPLE,因此,下面if语句不会执行:
  1.    if (((launchFlags&Intent.FLAG_ACTIVITY_NEW_TASK) != 0 &&  
  2.        (launchFlags&Intent.FLAG_ACTIVITY_MULTIPLE_TASK) == 0)  
  3.        || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK  
  4. || r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {  
  5. ......  
  6.    }  
        于是,变量addingToTask为false。

         继续往下看:

  1.    if (r.packageName != null) {  
  2. // If the activity being launched is the same as the one currently   
  3. // at the top, then we need to check if it should only be launched   
  4. // once.   
  5.        ActivityRecord top = topRunningNonDelayedActivityLocked(notTop);  
  6. if (top != null && r.resultTo == null) {  
  7.      if (top.realActivity.equals(r.realActivity)) {  
  8.     ......  
  9.      }  
  10. }  
  11.   
  12.    }   
        这里看一下当前要启动的Activity是否就是当前堆栈顶端的Activity,如果是的话,在某些情况下,就不用再重新启动了。函数topRunningNonDelayedActivityLocked返回当前
堆栈顶端的Activity,这里即为MainActivity,而当前要启动的Activity为SubActivity,因此,这二者不相等,于是跳过里面的if语句。

        接着往下执行:

  1.    // Should this be considered a new task?   
  2.    if (r.resultTo == null && !addingToTask  
  3. && (launchFlags&Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {  
  4. ......  
  5.   
  6.    } else if (sourceRecord != null) {  
  7. ......  
  8. // An existing activity is starting this new activity, so we want   
  9. // to keep the new one in the same task as the one that is starting   
  10. // it.   
  11. r.task = sourceRecord.task;  
  12. ......  
  13.   
  14.    } else {  
  15. ......  
  16.    }  
        前面说过参数intent的标志位Intent.FLAG_ACTIVITY_NEW_TASK没有设置,而这里的sourceRecord即为当前执行启动Activity操作的Activity,这里即为MainActivity,因此,它不为null,于是于MainActivity所属的Task设置到r.task中去,这里的r即为SubActivity。看到这里,我们就知道SubActivity要和MainActivity运行在同一个Task中了,同时,变量newTask的值为false。

        最后,函数进 入startActivityLocked(r, newTask, doResume)进一步处理了。这个函数同样是定义在frameworks/base/services/java/com/Android/server/am/ActivityStack.java文件中:

  1. public class ActivityStack {  
  2.   
  3.     ......  
  4.   
  5.     private final void startActivityLocked(ActivityRecord r, boolean newTask,  
  6.             boolean doResume) {  
  7.         final int NH = mHistory.size();  
  8.   
  9.         int addPos = -1;  
  10.   
  11.         if (!newTask) {  
  12.             // If starting in an existing task, find where that is...   
  13.             boolean startIt = true;  
  14.             for (int i = NH-1; i >= 0; i--) {  
  15.                 ActivityRecord p = (ActivityRecord)mHistory.get(i);  
  16.                 if (p.finishing) {  
  17.                     continue;  
  18.                 }  
  19.                 if (p.task == r.task) {  
  20.                     // Here it is!  Now, if this is not yet visible to the   
  21.                     // user, then just add it without starting; it will   
  22.                     // get started when the user navigates back to it.   
  23.                     addPos = i+1;  
  24.                     if (!startIt) {  
  25.                         mHistory.add(addPos, r);  
  26.                         r.inHistory = true;  
  27.                         r.task.numActivities++;  
  28.                         mService.mWindowManager.addAppToken(addPos, r, r.task.taskId,  
  29.                             r.info.screenOrientation, r.fullscreen);  
  30.                         if (VALIDATE_TOKENS) {  
  31.                             mService.mWindowManager.validateAppTokens(mHistory);  
  32.                         }  
  33.                         return;  
  34.                     }  
  35.                     break;  
  36.                 }  
  37.                 if (p.fullscreen) {  
  38.                     startIt = false;  
  39.                 }  
  40.             }  
  41.         }  
  42.   
  43.         ......  
  44.   
  45.         // Slot the activity into the history stack and proceed   
  46.         mHistory.add(addPos, r);  
  47.         r.inHistory = true;  
  48.         r.frontOfTask = newTask;  
  49.         r.task.numActivities++;  
  50.   
  51.         ......  
  52.   
  53.         if (doResume) {  
  54.             resumeTopActivityLocked(null);  
  55.         }  
  56.     }  
  57.   
  58.     ......  
  59.   
  60. }  
        这里传进来的参数newTask为false,doResume为true。当newTask为false,表示即将要启动的Activity是在原有的Task运行时,如果这个原有的Task当前对用户不可见时,这时候就不需要继续执行下去了,因为即使把这个Activity启动起来,用户也看不到,还不如先把它保存起来,等到下次这个Task对用户可见的时候,再启动不迟。这里,这个原有的Task,即运行MainActivity的Task当前对用户是可见的,因此,会继续往下执行。 linux
相关资讯       Android开发 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款