用Activity.moveTaskToBack()把当前任务放入后台,详细看注释:
/**
* Move the task containing this activity to the back of the activity
* stack. The activity's order within the task is unchanged.
* 把该activity所在的task移到栈底,顺序不变
* @param nonRoot If false then this only works if the activity is the root
* of a task; if true it will work for any activity in
* a task.
* nonRoot:false:当该activity是root activity才有用,true:所有的activity都有用
* @return If the task was moved (or it was already at the
* back) true is returned, else false.
*/
public boolean moveTaskToBack(boolean nonRoot) {
try {
return ActivityManagerNative.getDefault().moveActivityTaskToBack(
mToken, nonRoot);
} catch (RemoteException e) {
// Empty
}
return false;
}
上边的mToken是IBinder类型,代表该Activity与ActivityManagerService进行IPC通信(进程间通信),直接看ActivityManagerService源码,如下
/**
* Moves an activity, and all of the other activities within the same task, to the bottom
* of the history stack. The activity's order within the task is unchanged.
* 把该activity所在的task移到栈底,顺序不变
* @param token A reference to the activity we wish to move 保存activity的引用
* @param nonRoot If false then this only works if the activity is the root
* of a task; if true it will work for any activity in a task.
* nonRoot:false:当该activity是root activity才有用,true:所有的activity都有用
* @return Returns true if the move completed, false if not.
*/
public boolean moveActivityTaskToBack(IBinder token, boolean nonRoot) {
synchronized(this) {
final long origId = Binder.clearCallingIdentity();
int taskId = getTaskForActivityLocked(token, !nonRoot);
if (taskId >= 0) {
return mMainStack.moveTaskToBackLocked(taskId, null);
}
Binder.restoreCallingIdentity(origId);
}
return false;
}
getTaskForActivityLocked:
/**
* //找出token(activity)所在的栈
* @param token 在服务端为ActivityRecord,在客户端为Activity
* @param onlyRoot true 则只对root Activity有效,false对所有的Activity都有效
* @return
*/
int getTaskForActivityLocked(IBinder token, boolean onlyRoot) {
final int N = mMainStack.mHistory.size();
TaskRecord lastTask = null;
for (int i=0; i<N; i++) {
ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i);
if (r == token) {
if (!onlyRoot || lastTask != r.task) {//不是root Activity时返回第一次匹配的task,否则查找root Activity对应的task
return r.task.taskId;
}
return -1;
}
lastTask = r.task;
}
return -1;
}
mMainStack为ActivityStack,moveTaskToBackLocked如下:
/**
* Worker method for rearranging history stack. Implements the function of moving all
* activities for a specific task (gathering them if disjoint) into a single group at the
* bottom of the stack.
* 把特定的task移到栈底,并且保持顺序不变
* If a watcher is installed, the action is preflighted and the watcher has an opportunity
* to premeptively cancel the move.
*
* @param task The taskId to collect and move to the bottom.
* @return Returns true if the move completed, false if not.
*/
final boolean moveTaskToBackLocked(int task, ActivityRecord reason) {
....
//移动到底部
while (pos < N) {
ActivityRecord r = mHistory.get(pos);
if (localLOGV) Slog.v(
TAG, "At " + pos + " ckp " + r.task + ": " + r);
if (r.task.taskId == task) {
if (localLOGV) Slog.v(TAG, "Removing and adding at " + (N-1));
if (DEBUG_ADD_REMOVE) {
RuntimeException here = new RuntimeException("here");
here.fillInStackTrace();
Slog.i(TAG, "Removing and adding activity " + r + " to stack at "
+ bottom, here);
}
mHistory.remove(pos);
mHistory.add(bottom, r);
moved.add(r);
bottom++;
}
pos++;
}}
....
}