Mastering Android Activity Tasks and Launch Modes: A Deep Dive into Intent Flags
This article explains what an Android task is, how to manage tasks with different launch modes, how to inspect the current task stack via the device UI or adb, and provides detailed descriptions of common Intent flags and their effects on activity behavior.
What Is a Task
A task is a stack of Activity instances that the user interacts with while performing a specific piece of work. Each Activity is placed on the stack in the order it is opened.
How to Manage Tasks
The launch mode determines how a new Activity instance is associated with the current task. It can be configured in the <activity> element of the AndroidManifest.xml or by adding flags to the Intent passed to startActivity().
Viewing the Current System Task Stack
Tap the multitask button on the device to see a visual representation of the task stack.
From a command line run adb shell dumpsys activity and locate the "Running activities" section.
...
ACTIVITY MANAGER ACTIVITIES (dumpsys activity activities)
...
Running activities (most recent first):
TaskRecord{386481f #782 A=com.testintent_1 U=0 StackId=1 sz=1}
Run #1: ActivityRecord{e60b18e u0 com.testintent/.MainActivity t782}
TaskRecord{120576c #781 A=com.testintent U=0 StackId=1 sz=1}
Run #0: ActivityRecord{bdb842e u0 com.testintent/.CoverActivity t781}
...In this example there are two task stacks: com.testintent and com.testintent_1.
Activity Launch Modes
standard
Default mode. The system creates a new instance of the Activity in the task that launches it. Multiple instances can exist, each belonging to different tasks.
singleTop
If the top of the current task already hosts an instance of the Activity, the system delivers the new Intent to that instance via onNewIntent() instead of creating a new one. This mode only works when the existing instance is at the top of the stack.
When the activity is started with startActivityForResult, singleTop is ignored.
singleTask
The system searches for a task whose taskAffinity matches the requested one. If found, all activities above the existing instance are popped and the new Intent is delivered via onNewIntent(). If no such task exists, a new task is created and the activity is placed in it.
The taskAffinity attribute defaults to the application’s package name and is used together with singleTask or allowTaskReparenting.
If an activity from App A starts an activity from App B that has allowTaskReparenting="true", the activity can be moved from A’s task to B’s task.
When launched via startActivityForResult on Android 4.x the result is immediately RESULT_CANCELED; on Android 5.x the result is delivered normally.
singleInstance
Similar to singleTask but the task can contain only this one activity. Any activity started from it will open in a different task.
Using onNewIntent() Correctly
Inside onNewIntent() you must call setIntent(intent) manually; otherwise subsequent calls to getIntent() will return the old Intent.
Common Intent Flags
FLAG_ACTIVITY_NEW_TASK
* If set, this activity will become the start of a new task on this history stack.When this flag is used, if a task already exists for the activity, that task is brought to the front instead of creating a new instance. It cannot be used with startActivityForResult .
FLAG_ACTIVITY_CLEAR_TOP
* If the activity’s launch mode is "multiple" and FLAG_ACTIVITY_SINGLE_TOP is not set, the existing instance will be finished and recreated.If the activity is already in the stack, it is brought to the front and its onNewIntent() is called; otherwise it is recreated.
FLAG_ACTIVITY_SINGLE_TOP
* If set, the activity will not be launched if it is already running at the top of the history stack.This behaves like the singleTop launch mode.
FLAG_ACTIVITY_CLEAR_TASK
* When used with FLAG_ACTIVITY_NEW_TASK, any existing task associated with the activity is cleared before the activity is started.The activity becomes the new root of an otherwise empty task.
FLAG_ACTIVITY_REORDER_TO_FRONT
* If set, the launched activity is moved to the front of its task’s history stack if it is already running.Example: task A‑B‑C‑D, D starts B with this flag → order becomes A‑C‑D‑B.
FLAG_ACTIVITY_FORWARD_RESULT
* If set and the intent launches a new activity from an existing one, the result target of the existing activity is transferred to the new activity.The new activity can call setResult() and the result will be sent back to the original caller.
FLAG_ACTIVITY_PREVIOUS_IS_TOP
* If set, the activity that was before the current one is treated as the top, and the current activity finishes immediately.Often used together with FLAG_ACTIVITY_FORWARD_RESULT .
FLAG_ACTIVITY_NO_HISTORY
* If set, the new activity is not kept in the history stack; it is finished as soon as the user leaves it.This also prevents onActivityResult() from being invoked.
FLAG_ACTIVITY_TASK_ON_HOME
* If set with FLAG_ACTIVITY_NEW_TASK, the new task is placed on top of the current home activity task.Pressing back from this task always returns the user to the home screen.
FLAG_EXCLUDE_STOPPED_PACKAGES
* If set, the intent will not match any components in packages that are currently stopped.If not set, stopped packages are included in the resolution result.
Reference
https://developer.android.com/guide/components/activities/tasks-and-back-stack?hl=zh-cn
Android API 29 Platform
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Huajiao Technology
The Huajiao Technology channel shares the latest Huajiao app tech on an irregular basis, offering a learning and exchange platform for tech enthusiasts.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
