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

解开Android应用程序组件Activity的“singleTask”之谜

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

前面我们在两篇文章Android应用程序启动过程源代码分析和Android应用程序内部启动Activity过程(startActivity)的源代码分析时,分别在Step 9和Step 8中分析了Activity在启动过程中与任务相关的函数ActivityStack.startActivityUncheckedLocked函数中,它定义在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.         ActivityRecord notTop = (launchFlags&Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP)  
  16.            != 0 ? r : null;  
  17.   
  18.         ......  
  19.   
  20.         if (sourceRecord == null) {  
  21.             ......  
  22.         } else if (sourceRecord.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {  
  23.             ......  
  24.         } else if (r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE  
  25.            || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK) {  
  26.             // The activity being started is a single instance...  it always   
  27.             // gets launched into its own task.   
  28.             launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;  
  29.         }  
  30.   
  31.         ......  
  32.   
  33.         boolean addingToTask = false;  
  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.                // If bring to front is requested, and no result is requested, and   
  39.                // we can find a task that was started with this same   
  40.                // component, then instead of launching bring that one to the front.   
  41.                if (r.resultTo == null) {  
  42.                    // See if there is a task to bring to the front.  If this is   
  43.                    // a SINGLE_INSTANCE activity, there can be one and only one   
  44.                    // instance of it in the history, and it is always in its own   
  45.                    // unique task, so we do a special search.   
  46.                    ActivityRecord taskTop = r.launchMode != ActivityInfo.LAUNCH_SINGLE_INSTANCE  
  47.                        ? findTaskLocked(intent, r.info)  
  48.                        : findActivityLocked(intent, r.info);  
  49.                    if (taskTop != null) {  
  50.                          
  51.                        ......  
  52.   
  53.                        if ((launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0  
  54.                            || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK  
  55.                            || r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {  
  56.                                // In this situation we want to remove all activities   
  57.                                // from the task up to the one being started.  In most   
  58.                                // cases this means we are resetting the task to its   
  59.                                // initial state.   
  60.                                ActivityRecord top = performClearTaskLocked(  
  61.                                    taskTop.task.taskId, r, launchFlags, true);  
  62.                                if (top != null) {  
  63.                                    ......  
  64.                                } else {  
  65.                                    // A special case: we need to   
  66.                                    // start the activity because it is not currently   
  67.                                    // running, and the caller has asked to clear the   
  68.                                    // current task to have this activity at the top.   
  69.                                    addingToTask = true;  
  70.                                    // Now pretend like this activity is being started   
  71.                                    // by the top of its task, so it is put in the   
  72.                                    // right place.   
  73.                                    sourceRecord = taskTop;  
  74.                                }  
  75.                        } else if (r.realActivity.equals(taskTop.task.realActivity)) {  
  76.                            ......  
  77.                        } else if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) == 0) {  
  78.                            ......  
  79.                        } else if (!taskTop.task.rootWasReset) {  
  80.                            ......  
  81.                        }  
  82.                          
  83.                        ......  
  84.                    }  
  85.                }  
  86.         }  
  87.   
  88.         ......  
  89.   
  90.         if (r.packageName != null) {  
  91.            // If the activity being launched is the same as the one currently   
  92.            // at the top, then we need to check if it should only be launched   
  93.            // once.   
  94.            ActivityRecord top = topRunningNonDelayedActivityLocked(notTop);  
  95.            if (top != null && r.resultTo == null) {  
  96.                if (top.realActivity.equals(r.realActivity)) {  
  97.                    if (top.app != null && top.app.thread != null) {  
  98.                        ......  
  99.                    }  
  100.                }  
  101.            }  
  102.   
  103.         } else {  
  104.            ......  
  105.         }  
  106.   
  107.         boolean newTask = false;  
  108.   
  109.         // Should this be considered a new task?   
  110.         if (r.resultTo == null && !addingToTask  
  111.            && (launchFlags&Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {  
  112.              // todo: should do better management of integers.   
  113.                          mService.mCurTask++;  
  114.                          if (mService.mCurTask <= 0) {  
  115.                               mService.mCurTask = 1;  
  116.                          }  
  117.                          r.task = new TaskRecord(mService.mCurTask, r.info, intent,  
  118.                              (r.info.flags&ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0);  
  119.                          if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r  
  120.                             + " in new task " + r.task);  
  121.                          newTask = true;  
  122.                          if (mMainStack) {  
  123.                               mService.addRecentTaskLocked(r.task);  
  124.                          }  
  125.         } else if (sourceRecord != null) {  
  126.            if (!addingToTask &&  
  127.                (launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {  
  128.                 ......  
  129.            } else if (!addingToTask &&  
  130.                (launchFlags&Intent.FLAG_ACTIVITY_REORDER_TO_FRONT) != 0) {  
  131.                 ......  
  132.            }  
  133.            // An existing activity is starting this new activity, so we want   
  134.            // to keep the new one in the same task as the one that is starting   
  135.            // it.   
  136.            r.task = sourceRecord.task;  
  137.              
  138.            ......  
  139.   
  140.         } else {  
  141.            ......  
  142.         }  
  143.   
  144.         ......  
  145.   
  146.         startActivityLocked(r, newTask, doResume);  
  147.         return START_SUCCESS;  
  148.     }  
  149.   
  150.     ......  
  151.   
  152. }  

 首先是获得用来启动Activity的Intent的Flags,并且保存在launchFlags变量中,这里,launcFlags的Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP位没有置位,因此,notTop为null。

        接下来的这个if语句:

  1.    if (sourceRecord == null) {  
  2. ......  
  3.    } else if (sourceRecord.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {  
  4. ......  
  5.    } else if (r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE  
  6.       || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK) {  
  7. // The activity being started is a single instance...  it always   
  8. // gets launched into its own task.   
  9. launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;  
  10.    }  

        这里变量r的类型为ActivityRecord,它表示即将在启动的Activity,在这个例子中,即为SubActivity,因此,这里的r.launchMode等于ActivityInfo.LAUNCH_SINGLE_TASK,于是,无条件将launchFlags的Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP位置为1,表示这个SubActivity要在新的任务中启动,但是别急,还要看看其它条件是否满足,如果条件都满足,才可以在新的任务中启动这个SubActivity。
        接下将addingToTask变量初始化为false,这个变量也将决定是否要将SubActivity在新的任务中启动,从名字我们就可以看出,默认不增加到原有的任务中启动,即要在新的任务中启动。这里的r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK条成立,条件r.resultTo == null也成立,它表这个Activity不需要将结果返回给启动它的Activity。于是会进入接下来的if语句中,执行:

  1.   ActivityRecord taskTop = r.launchMode != ActivityInfo.LAUNCH_SINGLE_INSTANCE  
  2. ? findTaskLocked(intent, r.info)  
  3. : findActivityLocked(intent, r.info)  

        这里的条件r.launchMode != ActivityInfo.LAUNCH_SINGLE_INSTANCE成立,于是执行findTaskLocked函数,这个函数也是定义在frameworks/base/services/java/com/android/server/am/ActivityStack.java文件中:

  1. public class ActivityStack {  
  2.   
  3.     ......  
  4.   
  5.     /** 
  6.     * Returns the top activity in any existing task matching the given 
  7.     * Intent.  Returns null if no such task is found. 
  8.     */  
  9.     private ActivityRecord findTaskLocked(Intent intent, ActivityInfo info) {  
  10.         ComponentName cls = intent.getComponent();  
  11.         if (info.targetActivity != null) {  
  12.             cls = new ComponentName(info.packageName, info.targetActivity);  
  13.         }  
  14.   
  15.         TaskRecord cp = null;  
  16.   
  17.         final int N = mHistory.size();  
  18.         for (int i=(N-1); i>=0; i--) {  
  19.             ActivityRecord r = (ActivityRecord)mHistory.get(i);  
  20.             if (!r.finishing && r.task != cp  
  21.                 && r.launchMode != ActivityInfo.LAUNCH_SINGLE_INSTANCE) {  
  22.                     cp = r.task;  
  23.                     //Slog.i(TAG, "Comparing existing cls=" + r.task.intent.getComponent().flattenToShortString()   
  24.                     //        + "/aff=" + r.task.affinity + " to new cls="   
  25.                     //        + intent.getComponent().flattenToShortString() + "/aff=" + taskAffinity);   
  26.                     if (r.task.affinity != null) {  
  27.                         if (r.task.affinity.equals(info.taskAffinity)) {  
  28.                             //Slog.i(TAG, "Found matching affinity!");   
  29.                             return r;  
  30.                         }  
  31.                     } else if (r.task.intent != null  
  32.                         && r.task.intent.getComponent().equals(cls)) {  
  33.                             //Slog.i(TAG, "Found matching class!");   
  34.                             //dump();   
  35.                             //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent);   
  36.                             return r;  
  37.                     } else if (r.task.affinityIntent != null  
  38.                         && r.task.affinityIntent.getComponent().equals(cls)) {  
  39.                             //Slog.i(TAG, "Found matching class!");   
  40.                             //dump();   
  41.                             //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent);   
  42.                             return r;  
  43.                     }  
  44.             }  
  45.         }  
  46.   
  47.         return null;  
  48.     }  
  49.   
  50.     ......  
  51.   
  52. }  
linux
相关资讯       Android开发 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

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