Android setTaskFromReuseOrCreateNewTask 源码分析
时间: 2024-05-13 17:17:03 浏览: 14
`setTaskFromReuseOrCreateNewTask` 是 `ActivityOptions` 类中的一个方法,用于设置 Activity 的启动方式。该方法的作用是:如果当前栈中存在该 Activity,则将其从栈中移除并重新创建;如果当前栈中不存在该 Activity,则创建新的任务栈并将该 Activity 放入其中。
下面是该方法的源码分析:
```java
public ActivityOptions setTaskFromReuseOrCreateNewTask(boolean flag) {
if (flag) {
mAnimationType = ANIM_LAUNCH_TASK_BEHIND;
} else {
mAnimationType = ANIM_DEFAULT;
}
return this;
}
```
该方法接收一个 boolean 类型的参数 `flag`,用于指示是否重用任务栈。如果 `flag` 为 true,则将 `mAnimationType` 设置为 `ANIM_LAUNCH_TASK_BEHIND`,表示重新使用任务栈;如果 `flag` 为 false,则将 `mAnimationType` 设置为 `ANIM_DEFAULT`,表示创建新的任务栈。
其中,`mAnimationType` 是一个 int 类型的变量,用于存储 Activity 启动时的动画类型,它的值只能是以下常量之一:
```java
private static final int ANIM_NONE = 0; // 无动画
private static final int ANIM_CUSTOM = 1; // 自定义动画
private static final int ANIM_SCALE_UP = 2; // 缩放动画
private static final int ANIM_THUMBNAIL_SCALE_UP = 3; // 缩略图缩放动画
private static final int ANIM_THUMBNAIL_SCALE_DOWN = 4; // 缩略图缩小动画
private static final int ANIM_SCENE_TRANSITION = 5; // 场景转换动画
private static final int ANIM_LAUNCH_TASK_BEHIND = 6; // 重用任务栈
private static final int ANIM_DEFAULT = ANIM_NONE; // 默认无动画
```
因此,可以看出,`setTaskFromReuseOrCreateNewTask` 方法的实现非常简单,只是设置了一个 int 类型的变量的值。具体的任务栈的创建和 Activity 的移除操作需要在 ActivityManagerService 中完成。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)