# TaskStackBuilder

Artifact: [androidx.core:core](https://developer.android.com/jetpack/androidx/releases/core) [View Source](https://cs.android.com/search?q=file:androidx/core/app/TaskStackBuilder.java+class:androidx.core.app.TaskStackBuilder) Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)

*** ** * ** ***


```
public final class TaskStackBuilder implements Iterable<Intent>
```

<br />

*** ** * ** ***

Utility class for constructing synthetic back stacks for cross-task navigation on Android 3.0 and newer.

In API level 11 (Android 3.0/Honeycomb) the recommended conventions for app navigation using the back key changed. The back key's behavior is local to the current task and does not capture navigation across different tasks. Navigating across tasks and easily reaching the previous task is accomplished through the "recents" UI, accessible through the software-provided Recents key on the navigation or system bar. On devices with the older hardware button configuration the recents UI can be accessed with a long press on the Home key.

When crossing from one task stack to another post-Android 3.0, the application should synthesize a back stack/history for the new task so that the user may navigate out of the new task and back to the Launcher by repeated presses of the back key. Back key presses should not navigate across task stacks.

TaskStackBuilder provides a backward-compatible way to obey the correct conventions around cross-task navigation on the device's version of the platform. On devices running Android 3.0 or newer, calls to the `https://developer.android.com/reference/androidx/core/app/TaskStackBuilder#startActivities()` method or sending the `https://developer.android.com/reference/android/app/PendingIntent.html` generated by `https://developer.android.com/reference/androidx/core/app/TaskStackBuilder#getPendingIntent(int,int)` will construct the synthetic back stack as prescribed. On devices running older versions of the platform, these same calls will invoke the topmost activity in the supplied stack, ignoring the rest of the synthetic stack and allowing the back key to navigate back to the previous task.
### About Navigation For more detailed information about tasks, the back stack, and navigation design guidelines, please read from the developer guide and from the design guide.

## Summary

| ### Nested types |
|---|
| `public interface https://developer.android.com/reference/androidx/core/app/TaskStackBuilder.SupportParentable` |

| ### Public methods |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/core/app/TaskStackBuilder` | `https://developer.android.com/reference/androidx/core/app/TaskStackBuilder#addNextIntent(android.content.Intent)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/Intent.html nextIntent)` Add a new Intent to the task stack. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/core/app/TaskStackBuilder` | `https://developer.android.com/reference/androidx/core/app/TaskStackBuilder#addNextIntentWithParentStack(android.content.Intent)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/Intent.html nextIntent)` Add a new Intent with the resolved chain of parents for the target activity to the task stack. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/core/app/TaskStackBuilder` | `https://developer.android.com/reference/androidx/core/app/TaskStackBuilder#addParentStack(android.app.Activity)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/app/Activity.html sourceActivity)` Add the activity parent chain as specified by manifest elements to the task stack builder. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/core/app/TaskStackBuilder` | `https://developer.android.com/reference/androidx/core/app/TaskStackBuilder#addParentStack(java.lang.Class<?>)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/lang/Class.html<@https://developer.android.com/reference/androidx/annotation/NonNull ?> sourceActivityClass)` Add the activity parent chain as specified by manifest elements to the task stack builder. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/core/app/TaskStackBuilder` | `https://developer.android.com/reference/androidx/core/app/TaskStackBuilder#addParentStack(android.content.ComponentName)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/ComponentName.html sourceActivityName)` Add the activity parent chain as specified by manifest elements to the task stack builder. |
| `static @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/core/app/TaskStackBuilder` | `https://developer.android.com/reference/androidx/core/app/TaskStackBuilder#create(android.content.Context)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/Context.html context)` Return a new TaskStackBuilder for launching a fresh task stack consisting of a series of activities. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/android/content/Intent.html` | `https://developer.android.com/reference/androidx/core/app/TaskStackBuilder#editIntentAt(int)(int index)` Return the intent at the specified index for modification. |
| `static https://developer.android.com/reference/androidx/core/app/TaskStackBuilder` | `[from](https://developer.android.com/reference/androidx/core/app/TaskStackBuilder#from(android.content.Context))(https://developer.android.com/reference/android/content/Context.html context)` **This method is deprecated.** use `https://developer.android.com/reference/androidx/core/app/TaskStackBuilder#create(android.content.Context)` instead <br /> |
| `https://developer.android.com/reference/android/content/Intent.html` | `[getIntent](https://developer.android.com/reference/androidx/core/app/TaskStackBuilder#getIntent(int))(int index)` **This method is deprecated.** Renamed to editIntentAt to better reflect intended usage <br /> |
| `int` | `https://developer.android.com/reference/androidx/core/app/TaskStackBuilder#getIntentCount()()` |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html Intent[]` | `https://developer.android.com/reference/androidx/core/app/TaskStackBuilder#getIntents()()` Return an array containing the intents added to this builder. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/android/app/PendingIntent.html` | `https://developer.android.com/reference/androidx/core/app/TaskStackBuilder#getPendingIntent(int,int)(int requestCode, int flags)` Obtain a `https://developer.android.com/reference/android/app/PendingIntent.html` for launching the task constructed by this builder so far. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/android/app/PendingIntent.html` | `https://developer.android.com/reference/androidx/core/app/TaskStackBuilder#getPendingIntent(int,int,boolean)(int requestCode, int flags, boolean isMutable)` Obtains a `https://developer.android.com/reference/android/app/PendingIntent.html` with mandatory mutability flag set on supported platform versions. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/android/app/PendingIntent.html` | `https://developer.android.com/reference/androidx/core/app/TaskStackBuilder#getPendingIntent(int,int,android.os.Bundle)(int requestCode, int flags, @https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/android/os/Bundle.html options)` Obtain a `https://developer.android.com/reference/android/app/PendingIntent.html` for launching the task constructed by this builder so far. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/android/app/PendingIntent.html` | `https://developer.android.com/reference/androidx/core/app/TaskStackBuilder#getPendingIntent(int,int,android.os.Bundle,boolean)( int requestCode, int flags, @https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/android/os/Bundle.html options, boolean isMutable )` Obtains a `https://developer.android.com/reference/android/app/PendingIntent.html` with mandatory mutability flag set on supported platform versions. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/util/Iterator.html<https://developer.android.com/reference/android/content/Intent.html>` | `[iterator](https://developer.android.com/reference/androidx/core/app/TaskStackBuilder#iterator())()` **This method is deprecated.** Use editIntentAt instead <br /> |
| `void` | `https://developer.android.com/reference/androidx/core/app/TaskStackBuilder#startActivities()()` Start the task stack constructed by this builder. |
| `void` | `https://developer.android.com/reference/androidx/core/app/TaskStackBuilder#startActivities(android.os.Bundle)(@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/android/os/Bundle.html options)` Start the task stack constructed by this builder. |

### Inherited methods

From [java.lang.Iterable](https://developer.android.com/reference/java/lang/Iterable.html) `void` `https://developer.android.com/reference/java/lang/Iterable.html#forEach(java.util.function.Consumer&lt;? super T&gt;)(https://developer.android.com/reference/java/util/function/Consumer.html<T> action)` `https://developer.android.com/reference/java/util/Spliterator.html<T>` `https://developer.android.com/reference/java/lang/Iterable.html#spliterator()()`

## Public methods

### addNextIntent

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)

```
public @NonNull TaskStackBuilder addNextIntent(@NonNull Intent nextIntent)
```

Add a new Intent to the task stack. The most recently added Intent will invoke the Activity at the top of the final task stack.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/Intent.html nextIntent` | Intent for the next Activity in the synthesized task stack |

| Returns |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/core/app/TaskStackBuilder` | This TaskStackBuilder for method chaining |

### addNextIntentWithParentStack

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)

```
public @NonNull TaskStackBuilder addNextIntentWithParentStack(@NonNull Intent nextIntent)
```

Add a new Intent with the resolved chain of parents for the target activity to the task stack.

This is equivalent to calling `https://developer.android.com/reference/androidx/core/app/TaskStackBuilder#addParentStack(android.content.ComponentName)` with the resolved ComponentName of nextIntent (if it can be resolved), followed by `https://developer.android.com/reference/androidx/core/app/TaskStackBuilder#addNextIntent(android.content.Intent)` with nextIntent.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/Intent.html nextIntent` | Intent for the topmost Activity in the synthesized task stack. Its chain of parents as specified in the manifest will be added. |

| Returns |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/core/app/TaskStackBuilder` | This TaskStackBuilder for method chaining. |

### addParentStack

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)

```
public @NonNull TaskStackBuilder addParentStack(@NonNull Activity sourceActivity)
```

Add the activity parent chain as specified by manifest elements to the task stack builder.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/app/Activity.html sourceActivity` | All parents of this activity will be added |

| Returns |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/core/app/TaskStackBuilder` | This TaskStackBuilder for method chaining |

### addParentStack

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)

```
public @NonNull TaskStackBuilder addParentStack(@NonNull Class<@NonNull ?> sourceActivityClass)
```

Add the activity parent chain as specified by manifest elements to the task stack builder.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/lang/Class.html<@https://developer.android.com/reference/androidx/annotation/NonNull ?> sourceActivityClass` | All parents of this activity will be added |

| Returns |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/core/app/TaskStackBuilder` | This TaskStackBuilder for method chaining |

### addParentStack

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)

```
public @NonNull TaskStackBuilder addParentStack(@NonNull ComponentName sourceActivityName)
```

Add the activity parent chain as specified by manifest elements to the task stack builder.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/ComponentName.html sourceActivityName` | Must specify an Activity component. All parents of this activity will be added |

| Returns |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/core/app/TaskStackBuilder` | This TaskStackBuilder for method chaining |

### create

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)

```
public static @NonNull TaskStackBuilder create(@NonNull Context context)
```

Return a new TaskStackBuilder for launching a fresh task stack consisting of a series of activities.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/Context.html context` | The context that will launch the new task stack or generate a PendingIntent |

| Returns |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/core/app/TaskStackBuilder` | A new TaskStackBuilder |

### editIntentAt

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)

```
public @Nullable Intent editIntentAt(int index)
```

Return the intent at the specified index for modification. Useful if you need to modify the flags or extras of an intent that was previously added, for example with `https://developer.android.com/reference/androidx/core/app/TaskStackBuilder#addParentStack(android.app.Activity)`.

| Parameters |
|---|---|
| `int index` | Index from 0-getIntentCount() |

| Returns |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/android/content/Intent.html` | the intent at position index |

### from

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0) Deprecated in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)

```
public static TaskStackBuilder [from](https://developer.android.com/reference/androidx/core/app/TaskStackBuilder#from(android.content.Context))(Context context)
```

> [!CAUTION]
> **This method is deprecated.**   
>
> use `https://developer.android.com/reference/androidx/core/app/TaskStackBuilder#create(android.content.Context)` instead

Return a new TaskStackBuilder for launching a fresh task stack consisting of a series of activities.

| Parameters |
|---|---|
| `https://developer.android.com/reference/android/content/Context.html context` | The context that will launch the new task stack or generate a PendingIntent |

| Returns |
|---|---|
| `https://developer.android.com/reference/androidx/core/app/TaskStackBuilder` | A new TaskStackBuilder |

### getIntent

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0) Deprecated in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)

```
public Intent [getIntent](https://developer.android.com/reference/androidx/core/app/TaskStackBuilder#getIntent(int))(int index)
```

> [!CAUTION]
> **This method is deprecated.**   
>
> Renamed to editIntentAt to better reflect intended usage

Get the intent at the specified index. Useful if you need to modify the flags or extras of an intent that was previously added, for example with `https://developer.android.com/reference/androidx/core/app/TaskStackBuilder#addParentStack(android.app.Activity)`.

| Parameters |
|---|---|
| `int index` | Index from 0-getIntentCount() |

| Returns |
|---|---|
| `https://developer.android.com/reference/android/content/Intent.html` | the intent at position index |

### getIntentCount

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)

```
public int getIntentCount()
```

| Returns |
|---|---|
| `int` | the number of intents added so far. |

### getIntents

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)

```
public @NonNull Intent[] getIntents()
```

Return an array containing the intents added to this builder. The intent at the root of the task stack will appear as the first item in the array and the intent at the top of the stack will appear as the last item.

| Returns |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html Intent[]` | An array containing the intents added to this builder. |

### getPendingIntent

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)

```
public @Nullable PendingIntent getPendingIntent(int requestCode, int flags)
```

Obtain a `https://developer.android.com/reference/android/app/PendingIntent.html` for launching the task constructed by this builder so far.

| Parameters |
|---|---|
| `int requestCode` | Private request code for the sender |
| `int flags` | May be `https://developer.android.com/reference/android/app/PendingIntent.html#FLAG_ONE_SHOT`, `https://developer.android.com/reference/android/app/PendingIntent.html#FLAG_NO_CREATE`, `https://developer.android.com/reference/android/app/PendingIntent.html#FLAG_CANCEL_CURRENT`, `https://developer.android.com/reference/android/app/PendingIntent.html#FLAG_UPDATE_CURRENT`, or any of the flags supported by `https://developer.android.com/reference/android/content/Intent.html#fillIn(android.content.Intent, int)` to control which unspecified parts of the intent that can be supplied when the actual send happens. |

| Returns |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/android/app/PendingIntent.html` | The obtained PendingIntent. May return null only if `https://developer.android.com/reference/android/app/PendingIntent.html#FLAG_NO_CREATE` has been supplied. |

### getPendingIntent

Added in [1.16.0](https://developer.android.com/jetpack/androidx/releases/core#1.16.0)

```
public @Nullable PendingIntent getPendingIntent(int requestCode, int flags, boolean isMutable)
```

Obtains a `https://developer.android.com/reference/android/app/PendingIntent.html` with mandatory mutability flag set on supported platform versions. The caller provides the flag as combination of all the other values except mutability flag. This method combines mutability flag when necessary. See `https://developer.android.com/reference/androidx/core/app/TaskStackBuilder#getPendingIntent(int,int)`.

### getPendingIntent

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)

```
public @Nullable PendingIntent getPendingIntent(int requestCode, int flags, @Nullable Bundle options)
```

Obtain a `https://developer.android.com/reference/android/app/PendingIntent.html` for launching the task constructed by this builder so far.

| Parameters |
|---|---|
| `int requestCode` | Private request code for the sender |
| `int flags` | May be `https://developer.android.com/reference/android/app/PendingIntent.html#FLAG_ONE_SHOT`, `https://developer.android.com/reference/android/app/PendingIntent.html#FLAG_NO_CREATE`, `https://developer.android.com/reference/android/app/PendingIntent.html#FLAG_CANCEL_CURRENT`, `https://developer.android.com/reference/android/app/PendingIntent.html#FLAG_UPDATE_CURRENT`, or any of the flags supported by `https://developer.android.com/reference/android/content/Intent.html#fillIn(android.content.Intent, int)` to control which unspecified parts of the intent that can be supplied when the actual send happens. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/android/os/Bundle.html options` | Additional options for how the Activity should be started. See `https://developer.android.com/reference/android/content/Context.html#startActivity(android.content.Intent, android.os.Bundle)` |

| Returns |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/android/app/PendingIntent.html` | The obtained PendingIntent. May return null only if `https://developer.android.com/reference/android/app/PendingIntent.html#FLAG_NO_CREATE` has been supplied. |

### getPendingIntent

Added in [1.16.0](https://developer.android.com/jetpack/androidx/releases/core#1.16.0)

```
public @Nullable PendingIntent getPendingIntent(
    int requestCode,
    int flags,
    @Nullable Bundle options,
    boolean isMutable
)
```

Obtains a `https://developer.android.com/reference/android/app/PendingIntent.html` with mandatory mutability flag set on supported platform versions. The caller provides the flag as combination of all the other values except mutability flag. This method combines mutability flag when necessary. See `https://developer.android.com/reference/androidx/core/app/TaskStackBuilder#getPendingIntent(int,int,android.os.Bundle)`.

### iterator

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0) Deprecated in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)

```
public @NonNull Iterator<Intent> [iterator](https://developer.android.com/reference/androidx/core/app/TaskStackBuilder#iterator())()
```

> [!CAUTION]
> **This method is deprecated.**   
>
> Use editIntentAt instead

### startActivities

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)

```
public void startActivities()
```

Start the task stack constructed by this builder. The Context used to obtain this builder must be an Activity.

On devices that do not support API level 11 or higher the topmost activity will be started as a new task. On devices that do support API level 11 or higher the new task stack will be created in its entirety.

### startActivities

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)

```
public void startActivities(@Nullable Bundle options)
```

Start the task stack constructed by this builder. The Context used to obtain this builder must be an Activity.

On devices that do not support API level 11 or higher the topmost activity will be started as a new task. On devices that do support API level 11 or higher the new task stack will be created in its entirety.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/android/os/Bundle.html options` | Additional options for how the Activity should be started. See `https://developer.android.com/reference/android/content/Context.html#startActivity(android.content.Intent, android.os.Bundle)` |