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

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

[日期:2011-08-29] 来源:Linux社区  作者:luoshengyang [字体:大 中 小]
这个函数无非就是根据即将要启动的SubActivity的taskAffinity属性值在系统中查找这样的一个Task:Task的affinity属性值与即将要启动的Activity的taskAffinity属性值一致。如果存在,就返回这个Task堆栈顶端的Activity回去。在上面的AndroidManifest.xml文件中,没有配置MainActivity和SubActivity的taskAffinity属性,于是它们的taskAffinity属性值就默认为父标签application的taskAffinity属性值,这里,标签application的taskAffinity也没有配置,于是它们就默认为包名,即"shy.luo.task"。由于在启动SubActivity之前,MainActivity已经启动,MainActivity启动的时候,会在一个新的任务里面启动,而这个新的任务的affinity属性就等于它的第一个Activity的taskAffinity属性值。于是,这个函数会动回表示MainActivity的ActivityRecord回去.

        回到前面的startActivityUncheckedLocked函数中,这里的taskTop就表示MainActivity,它不为null,于是继续往前执行。由于条件r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK成立,于是执行下面语句:

  1. ActivityRecord top = performClearTaskLocked(  
  2. kTop.task.taskId, r, launchFlags, true);  
        函数performClearTaskLocked也是定义在frameworks/base/services/java/com/android/server/am/ActivityStack.java文件中:
 
  1. public class ActivityStack {  
  2.   
  3.     ......  
  4.   
  5.     /** 
  6.     * Perform clear operation as requested by 
  7.     * {@link Intent#FLAG_ACTIVITY_CLEAR_TOP}: search from the top of the 
  8.     * stack to the given task, then look for 
  9.     * an instance of that activity in the stack and, if found, finish all 
  10.     * activities on top of it and return the instance. 
  11.     * 
  12.     * @param newR Description of the new activity being started. 
  13.     * @return Returns the old activity that should be continue to be used, 
  14.     * or null if none was found. 
  15.     */  
  16.     private final ActivityRecord performClearTaskLocked(int taskId,  
  17.     ActivityRecord newR, int launchFlags, boolean doClear) {  
  18.         int i = mHistory.size();  
  19.   
  20.         // First find the requested task.   
  21.         while (i > 0) {  
  22.             i--;  
  23.             ActivityRecord r = (ActivityRecord)mHistory.get(i);  
  24.             if (r.task.taskId == taskId) {  
  25.                 i++;  
  26.                 break;  
  27.             }  
  28.         }  
  29.   
  30.         // Now clear it.   
  31.         while (i > 0) {  
  32.             i--;  
  33.             ActivityRecord r = (ActivityRecord)mHistory.get(i);  
  34.             if (r.finishing) {  
  35.                 continue;  
  36.             }  
  37.             if (r.task.taskId != taskId) {  
  38.                 return null;  
  39.             }  
  40.             if (r.realActivity.equals(newR.realActivity)) {  
  41.                 // Here it is!  Now finish everything in front...   
  42.                 ActivityRecord ret = r;  
  43.                 if (doClear) {  
  44.                     while (i < (mHistory.size()-1)) {  
  45.                         i++;  
  46.                         r = (ActivityRecord)mHistory.get(i);  
  47.                         if (r.finishing) {  
  48.                             continue;  
  49.                         }  
  50.                         if (finishActivityLocked(r, i, Activity.RESULT_CANCELED,  
  51.                             null, "clear")) {  
  52.                                 i--;  
  53.                         }  
  54.                     }  
  55.                 }  
  56.   
  57.                 // Finally, if this is a normal launch mode (that is, not   
  58.                 // expecting onNewIntent()), then we will finish the current   
  59.                 // instance of the activity so a new fresh one can be started.   
  60.                 if (ret.launchMode == ActivityInfo.LAUNCH_MULTIPLE  
  61.                     && (launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) == 0) {  
  62.                         if (!ret.finishing) {  
  63.                             int index = indexOfTokenLocked(ret);  
  64.                             if (index >= 0) {  
  65.                                 finishActivityLocked(ret, index, Activity.RESULT_CANCELED,  
  66.                                     null, "clear");  
  67.                             }  
  68.                             return null;  
  69.                         }  
  70.                 }  
  71.   
  72.                 return ret;  
  73.             }  
  74.         }  
  75.   
  76.         return null;  
  77.     }  
  78.   
  79.     ......  
  80.   
  81. }  
        这个函数中作用无非就是找到ID等于参数taskId的任务,然后在这个任务中查找是否已经存在即将要启动的Activity的实例,如果存在,就会把这个Actvity实例上面直到任务堆栈顶端的Activity通过调用finishActivityLocked函数将它们结束掉。在这个例子中,就是要在属性值affinity等于"shy.luo.task"的任务中看看是否存在SubActivity类型的实例,如果有,就把它上面的Activity都结束掉。这里,属性值affinity等于"shy.luo.task"的任务只有一个MainActivity,而且它不是SubActivity的实例,所以这个函数就返回null了。

        回到前面的startActivityUncheckedLocked函数中,这里的变量top就为null了,于是执行下面的else语句:

  1.    if (top != null) {  
  2. ......  
  3.    } else {  
  4. // A special case: we need to   
  5. // start the activity because it is not currently   
  6. // running, and the caller has asked to clear the   
  7. // current task to have this activity at the top.   
  8. addingToTask = true;  
  9. // Now pretend like this activity is being started   
  10. // by the top of its task, so it is put in the   
  11. // right place.   
  12. sourceRecord = taskTop;  
  13.    }  
        于是,变量addingToTask值就为true了,同时将变量sourceRecord的值设置为taskTop,即前面调用findTaskLocked函数的返回值,这里,它就是表示MainActivity了。

        继续往下看,下面这个if语句:

  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.             if (top.app != null && top.app.thread != null) {  
  9.             ......  
  10.         }  
  11.     }  
  12. }  
  13.   
  14.    } else {  
  15. ......  
  16.    }  
        它是例行性地检查当前任务顶端的Activity,是否是即将启动的Activity的实例,如果是否的话,在某些情况下,它什么也不做,就结束这个函数调用了。这里,当前任务顶端的Activity为MainActivity,它不是SubActivity实例,于是继续往下执行:
  1.    boolean newTask = false;  
  2.   
  3.    // Should this be considered a new task?   
  4.    if (r.resultTo == null && !addingToTask  
  5. && (launchFlags&Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {  
  6. ......  
  7.   
  8.    } else if (sourceRecord != null) {  
  9. if (!addingToTask &&  
  10.     (launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {  
  11.     ......  
  12. } else if (!addingToTask &&  
  13.         (launchFlags&Intent.FLAG_ACTIVITY_REORDER_TO_FRONT) != 0) {  
  14.     ......  
  15. }  
  16. // An existing activity is starting this new activity, so we want   
  17. // to keep the new one in the same task as the one that is starting   
  18. // it.   
  19. r.task = sourceRecord.task;  
  20.          
  21. ......  
  22.   
  23.    } else {  
  24.        ......  
  25.    }  
        这里首先将newTask变量初始化为false,表示不要在新的任务中启动这个SubActivity。由于前面的已经把addingToTask设置为true,因此,这里会执行中间的else if语句,即这里会把r.task设置为sourceRecord.task,即把SubActivity放在MainActivity所在的任务中启动。linux
相关资讯       Android开发 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

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