moveTasktoBack 把当前任务放入后台

http://blog.csdn.net/newcman/article/details/7801400

[java]  view plain  copy
 
  1. 用Activity.moveTaskToBack()把当前任务放入后台,详细看注释:  
[java]  view plain  copy
 
  1. /** 
  2.  * Move the task containing this activity to the back of the activity 
  3.  * stack.  The activity's order within the task is unchanged. 
  4.  * 把该activity所在的task移到栈底,顺序不变 
  5.  * @param nonRoot If false then this only works if the activity is the root 
  6.  *                of a task; if true it will work for any activity in 
  7.  *                a task. 
  8.  * nonRoot:false:当该activity是root activity才有用,true:所有的activity都有用 
  9.  * @return If the task was moved (or it was already at the 
  10.  *         back) true is returned, else false. 
  11.  */  
  12. public boolean moveTaskToBack(boolean nonRoot) {  
  13.     try {  
  14.         return ActivityManagerNative.getDefault().moveActivityTaskToBack(  
  15.                 <span style="color:#ff6666;">mToken</span>, nonRoot);  
  16.     } catch (RemoteException e) {  
  17.         // Empty  
  18.     }  
  19.     return false;  
  20. }  

上边的mToken是IBinder类型,代表该Activity与ActivityManagerService进行IPC通信(进程间通信),直接看ActivityManagerService源码,如下

[java]  view plain  copy
 
  1. /** 
  2.  * Moves an activity, and all of the other activities within the same task, to the bottom 
  3.  * of the history stack.  The activity's order within the task is unchanged. 
  4.  *  把该activity所在的task移到栈底,顺序不变 
  5.  * @param token A reference to the activity we wish to move 保存activity的引用 
  6.  * @param nonRoot If false then this only works if the activity is the root 
  7.  *                of a task; if true it will work for any activity in a task. 
  8.  * nonRoot:false:当该activity是root activity才有用,true:所有的activity都有用 
  9.  * @return Returns true if the move completed, false if not. 
  10.  */  
  11. public boolean moveActivityTaskToBack(IBinder token, boolean nonRoot) {  
  12.     synchronized(this) {  
  13.         final long origId = Binder.clearCallingIdentity();  
  14.         int taskId = getTaskForActivityLocked(token, !nonRoot);  
  15.         if (taskId >= 0) {  
  16.             return mMainStack.moveTaskToBackLocked(taskId, null);  
  17.         }  
  18.         Binder.restoreCallingIdentity(origId);  
  19.     }  
  20.     return false;  
  21. }  


 

getTaskForActivityLocked:

[java]  view plain  copy
 
    1.     <pre class="java" name="code"> /** 
    2.      *  //找出token(activity)所在的栈 
    3.      * @param token 在服务端为ActivityRecord,在客户端为Activity 
    4.      * @param onlyRoot true 则只对root Activity有效,false对所有的Activity都有效 
    5.      * @return 
    6.      */  
    7.     int getTaskForActivityLocked(IBinder token, boolean onlyRoot) {  
    8.         final int N = mMainStack.mHistory.size();  
    9.         TaskRecord lastTask = null;  
    10.         for (int i=0; i<N; i++) {  
    11.             ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i);  
    12.             if (r == token) {  
    13.                 if (!onlyRoot || lastTask != r.task) {//不是root Activity时返回第一次匹配的task,否则查找root Activity对应的task  
    14.                     return r.task.taskId;  
    15.                 }  
    16.                 return -1;  
    17.             }  
    18.             lastTask = r.task;  
    19.         }  
    20.   
    21.         return -1;  
    22.     }  
    23. </pre><pre class="java" name="code"><pre></pre>  
    24. <p>mMainStack为ActivityStack,moveTaskToBackLocked如下:</p>  
    25. <pre class="html" name="code">    /** 
    26.      * Worker method for rearranging history stack.  Implements the function of moving all  
    27.      * activities for a specific task (gathering them if disjoint) into a single group at the  
    28.      * bottom of the stack. 
    29.      * 把特定的task移到栈底,并且保持顺序不变 
    30.      * If a watcher is installed, the action is preflighted and the watcher has an opportunity 
    31.      * to premeptively cancel the move. 
    32.      *  
    33.      * @param task The taskId to collect and move to the bottom. 
    34.      * @return Returns true if the move completed, false if not. 
    35.      */  
    36.     final boolean moveTaskToBackLocked(int task, ActivityRecord reason) {  
    37. </pre><pre class="html" name="code">    ....</pre><pre class="html" name="code"> //移动到底部  
    38.         while (pos < N) {  
    39.             ActivityRecord r = mHistory.get(pos);  
    40.             if (localLOGV) Slog.v(  
    41.                 TAG, "At " + pos + " ckp " + r.task + ": " + r);  
    42.             if (r.task.taskId == task) {  
    43.                 if (localLOGV) Slog.v(TAG, "Removing and adding at " + (N-1));  
    44.                 if (DEBUG_ADD_REMOVE) {  
    45.                     RuntimeException here = new RuntimeException("here");  
    46.                     here.fillInStackTrace();  
    47.                     Slog.i(TAG, "Removing and adding activity " + r + " to stack at "  
    48.                             + bottom, here);  
    49.                 }  
    50.                 mHistory.remove(pos);  
    51.                 mHistory.add(bottom, r);  
    52.                 moved.add(r);  
    53.                 bottom++;  
    54.             }  
    55.             pos++;  
    56.         }}</pre><pre class="html" name="code">   ....</pre><pre class="html" name="code">}  
    57. </pre>  
    58. <p><br>  
    59.  </p>  
    60. <pre></pre>  
    61. <pre></pre>  
    62.      
    63. </pre>  

转载于:https://www.cnblogs.com/zhengtu2015/p/5144884.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值