我们都知道Activity是放入任务栈中的,这里记录几个关于栈的方法,可能在某些业务中能用得上。
1、
/**
* Return the identifier of the task this activity is in. This identifier
* will remain the same for the lifetime of the activity.
*
* @return Task identifier, an opaque integer.
*/
public int getTaskId() {
try {
return ActivityTaskManager.getService().getTaskForActivity(mToken, false);
} catch (RemoteException e) {
return -1;
}
}
返回该Activity实例所在任务栈id,一般情况下,启动的多个Activity实例都在一个任务栈中,id也是一样的;
2、
/**
* Return whether this activity is the root of a task. The root is the
* first activity in a task.
*
* @return True if this is the root activity, else false.
*/
public boolean isTaskRoot() {
return mWindowControllerCallback.isTaskRoot();
}
返回该Activity实例是否在任务栈底;
3、
/**
* Indication of whether this is the highest level activity in this task. Can be used to
* determine whether an activity launched by this activity was placed in the same task or
* another task.
*
* @return true if this is the topmost, non-finishing activity in its task.
*/
final boolean isTopOfTask() {
if (mToken == null || mWindow == null) {
return false;
}
try {
return ActivityTaskManager.getService().isTopOfTask(getActivityToken());
} catch (RemoteException e) {
return false;
}
}
返回该Activity实例是否处于任务栈的顶端;
4、
/**
* Call this when your activity is done and should be closed and the task should be completely
* removed as a part of finishing the root activity of the task.
*/
public void finishAndRemoveTask() {
finish(FINISH_TASK_WITH_ROOT_ACTIVITY);
}
关闭该Activity实例以及所在任务栈,该Activity实例必须在栈底时才生效;
5、
/**
* Move the task containing this activity to the back of the activity
* stack. The activity's order within the task is unchanged.
*
* @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.
*
* @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 ActivityTaskManager.getService().moveActivityTaskToBack(mToken, nonRoot);
} catch (RemoteException e) {
// Empty
}
return false;
}
这个方法很神奇,移动该Activity实例所在的任务栈到后台(到该系统所有Activity任务栈之后),不会调用 onDestroy()方法,就相当于是人为的点下了home键切换到了桌面;
nonRoot:
此参数为false,只有该处于任务栈底时才生效,为true,对于该栈所有Activity都有效,有个疑问,如果都有效直接true好了,搞个false不是多此一举吗?难道是为了性能更好的选择?
如果移动成功,返回true,失败返回false;
6、
/**
* Sets information describing the task with this activity for presentation inside the Recents
* System UI. When {@link ActivityManager#getRecentTasks} is called, the activities of each task
* are traversed in order from the topmost activity to the bottommost. The traversal continues
* for each property until a suitable value is found. For each task the taskDescription will be
* returned in {@link android.app.ActivityManager.TaskDescription}.
*
* @see ActivityManager#getRecentTasks
* @see android.app.ActivityManager.TaskDescription
*
* @param taskDescription The TaskDescription properties that describe the task with this activity
*/
public void setTaskDescription(ActivityManager.TaskDescription taskDescription) {
if (mTaskDescription != taskDescription) {
mTaskDescription.copyFromPreserveHiddenFields(taskDescription);
// Scale the icon down to something reasonable if it is provided
if (taskDescription.getIconFilename() == null && taskDescription.getIcon() != null) {
final int size = ActivityManager.getLauncherLargeIconSizeInner(this);
final Bitmap icon = Bitmap.createScaledBitmap(taskDescription.getIcon(), size, size,
true);
mTaskDescription.setIcon(Icon.createWithBitmap(icon));
}
}
try {
ActivityTaskManager.getService().setTaskDescription(mToken, mTaskDescription);
} catch (RemoteException e) {
}
}
设置手机任务列表界面中,该Activity实例任务栈的样式,比如:标题,图标,背景色等等;
好了,又可以愉快玩耍了。