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

Android应用程序在新的进程中启动新的Activity的方法和过程分析

[日期:2011-08-29] 来源:Linux社区  作者:luoshengyang [字体:大 中 小]

与前面介绍的MainActivity启动过程相比,这里少了中间创建新的进程的步骤;接下来,我们就详细分析一下SubActivity与MainActivity启动过程中有差别的地方,相同的地方请参考Android应用程序启动过程源代码分析一文。

Step 1. Activity.startActivity

这一步与上一篇文章Android应用程序启动过程源代码分析的Step 2大体一致,通过指定名称“shy.luo.activity.subactivity”来告诉应用程序框架层,它要隐式地启动SubActivity。所不同的是传入的参数intent没有Intent.FLAG_ACTIVITY_NEW_TASK标志,表示这个SubActivity和启动它的MainActivity运行在同一个Task中。

Step 2. Activity.startActivityForResult

这一步与上一篇文章Android应用程序启动过程源代码分析的Step 3一致。

Step 3. Instrumentation.execStartActivity

这一步与上一篇文章Android应用程序启动过程源代码分析的Step 4一致。

Step 4. ActivityManagerProxy.startActivity

这一步与上一篇文章Android应用程序启动过程源代码分析的Step 5一致。

Step 5. ActivityManagerService.startActivity

这一步与上一篇文章Android应用程序启动过程源代码分析的Step 6一致。

Step 6. ActivityStack.startActivityMayWait

这一步与上一篇文章Android应用程序启动过程源代码分析的Step 7一致。

Step 7. ActivityStack.startActivityLocked

这一步与上一篇文章Android应用程序启动过程源代码分析的Step 8一致。

Step 8. ActivityStack.startActivityUncheckedLocked

这一步与上一篇文章Android应用程序启动过程源代码分析的Step 9有所不同,主要是当前要启动的Activity与启动它的Activity是在同一个Task中运行的,我们来详细看一下。这个函数定义在frameworks/base/services/java/com/Android/server/am/ActivityStack.java文件中:

  1. public class ActivityStack {  
  2.   
  3.     ......  
  4.   
  5.     final int startActivityUncheckedLocked(ActivityRecord r,  
  6.            ActivityRecord sourceRecord, Uri[] grantedUriPermissions,  
  7.            int grantedMode, boolean onlyIfNeeded, boolean doResume) {  
  8.         final Intent intent = r.intent;  
  9.         final int callingUid = r.launchedFromUid;  
  10.   
  11.         int launchFlags = intent.getFlags();  
  12.   
  13.         ......  
  14.   
  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.            ......  
  21.         }  
  22.   
  23.         if (r.resultTo != null && (launchFlags&Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {  
  24.            ......  
  25.         }  
  26.   
  27.         boolean addingToTask = false;  
  28.         if (((launchFlags&Intent.FLAG_ACTIVITY_NEW_TASK) != 0 &&  
  29.            (launchFlags&Intent.FLAG_ACTIVITY_MULTIPLE_TASK) == 0)  
  30.            || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK  
  31.            || r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {  
  32.             ......  
  33.         }  
  34.   
  35.         if (r.packageName != null) {  
  36.            // If the activity being launched is the same as the one currently   
  37.            // at the top, then we need to check if it should only be launched   
  38.            // once.   
  39.            ActivityRecord top = topRunningNonDelayedActivityLocked(notTop);  
  40.            if (top != null && r.resultTo == null) {  
  41.                if (top.realActivity.equals(r.realActivity)) {  
  42.                    ......  
  43.                }  
  44.            }  
  45.   
  46.         } else {  
  47.            ......  
  48.         }  
  49.   
  50.         boolean newTask = false;  
  51.   
  52.         // Should this be considered a new task?   
  53.         if (r.resultTo == null && !addingToTask  
  54.            && (launchFlags&Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {  
  55.             ......  
  56.   
  57.         } else if (sourceRecord != null) {  
  58.             ......  
  59.             // An existing activity is starting this new activity, so we want   
  60.             // to keep the new one in the same task as the one that is starting   
  61.             // it.   
  62.             r.task = sourceRecord.task;  
  63.             ......  
  64.   
  65.         } else {  
  66.            ......  
  67.         }  
  68.   
  69.         ......  
  70.   
  71.         startActivityLocked(r, newTask, doResume);  
  72.         return START_SUCCESS;  
  73.     }  
  74.   
  75.     ......  
  76.   
  77. }  
linux
相关资讯       Android开发 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

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