> For the complete documentation index, see [llms.txt](https://blog.cweihang.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://blog.cweihang.io/android/activity/intent_flag.md).

# Intent Flag

FLAG\_与LaunchMode相比最大的不同是临时性

**1.FLAG\_ACTIVITY\_NEW\_TASK：**

Developer.android.com的说法：

> （1）在新的task中启动这个Activity，
>
> （2）如果已有一个task包含这个Activity，则这个task被带到前台。
>
> （3）效果与SingleTask相同。

## **实验发现**

* 只有从外部（另外的App）启动Activity，并添加FLAG\_ACTIVITY\_NEW\_TASK标识时，表现才与上述相符；
* 但不具有singleTask的clear\_top效果。
* 而在同一个APP内启动Activity并添加FLAG\_ACTIVITY\_NEW\_TASK时，与不添加标识效果一样，不仅不会新建task，而且新建的Activity不具有唯一性，与singleTask不完全相同。
* 在同一App内启动Activity时，如果将new\_task和multi\_task组合使用，就会达到打开新task的效果。

**2.FLAG\_ACTIVITY\_SINGLE\_TOP**

打开的Activity如果在栈顶，则不创建新的实例，并且会触发onNewIntent事件。

与启动launchMode为SINGLE\_TOP的Activity一致。

**3.FLAG\_ACTIVITY\_CLEAR\_TOP**

Developer.android.com的说法：

> （1）如果当前task包含这个Activity，这个Activity以上的Activity出栈，这个Activity到达栈顶。
>
> （2）如果这个Activity是standard模式，这个Activity也出栈，并且重新实例化到达栈顶。

默认行为应该是清除包括这个Activity及其以上Activity的所有Activity，但如果为要启动的Activity设置了特殊的launchMode，则launchMode会影响这个Activity的销毁与否，即：

如果这个Activity是singleTop或singleTask模式，这个Activity不出栈。

singleInstance模式没有CLEAR\_TOP的意义，因为它的task中只有自己一个Activity。

**4.FLAG\_ACTIVITY\_REORDER\_TO\_FRONT**

如果当前task中包含这个Activity，这个Activity被拉到栈顶，其他Activity的顺序不变，仍在task中。如果这个Activity被设置为SingleTask或者打开这个Activity的时候，还添加了CLEAR\_TOP的标签，则会将这个Activity上面的Activity出栈。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://blog.cweihang.io/android/activity/intent_flag.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
