# 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)

*** ** * ** ***


```
class TaskStackBuilder : (Mutable)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/kotlin/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/kotlin/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 |
|---|
| `interface https://developer.android.com/reference/kotlin/androidx/core/app/TaskStackBuilder.SupportParentable` |

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

### Inherited functions

From [java.lang.Iterable](https://developer.android.com/reference/java/lang/Iterable.html) `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-iterable/for-each.html(action: https://developer.android.com/reference/java/util/function/Consumer.html<T!>!)` `https://developer.android.com/reference/java/util/Spliterator.html<T!>!` `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-iterable/spliterator.html()`

## Public functions

### addNextIntent

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

```
fun addNextIntent(nextIntent: Intent): TaskStackBuilder
```

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 |
|---|---|
| `nextIntent: https://developer.android.com/reference/android/content/Intent.html` | Intent for the next Activity in the synthesized task stack |

| Returns |
|---|---|
| `https://developer.android.com/reference/kotlin/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)

```
fun addNextIntentWithParentStack(nextIntent: Intent): TaskStackBuilder
```

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/kotlin/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/kotlin/androidx/core/app/TaskStackBuilder#addNextIntent(android.content.Intent)` with nextIntent.

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

| Returns |
|---|---|
| `https://developer.android.com/reference/kotlin/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)

```
fun addParentStack(sourceActivity: Activity): TaskStackBuilder
```

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

| Parameters |
|---|---|
| `sourceActivity: https://developer.android.com/reference/android/app/Activity.html` | All parents of this activity will be added |

| Returns |
|---|---|
| `https://developer.android.com/reference/kotlin/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)

```
fun addParentStack(sourceActivityClass: Class<*>): TaskStackBuilder
```

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

| Parameters |
|---|---|
| `sourceActivityClass: https://developer.android.com/reference/java/lang/Class.html<*>` | All parents of this activity will be added |

| Returns |
|---|---|
| `https://developer.android.com/reference/kotlin/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)

```
fun addParentStack(sourceActivityName: ComponentName): TaskStackBuilder
```

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

| Parameters |
|---|---|
| `sourceActivityName: https://developer.android.com/reference/android/content/ComponentName.html` | Must specify an Activity component. All parents of this activity will be added |

| Returns |
|---|---|
| `https://developer.android.com/reference/kotlin/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)

```
java-static fun create(context: Context): TaskStackBuilder
```

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

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

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

### editIntentAt

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

```
fun editIntentAt(index: Int): Intent?
```

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/kotlin/androidx/core/app/TaskStackBuilder#addParentStack(android.app.Activity)`.

| Parameters |
|---|---|
| `index: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | Index from 0-getIntentCount() |

| Returns |
|---|---|
| `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)

```
java-static fun [from](https://developer.android.com/reference/kotlin/androidx/core/app/TaskStackBuilder#from(android.content.Context))(context: Context!): TaskStackBuilder!
```

> [!CAUTION]
> **This function is deprecated.**   
>
> use `https://developer.android.com/reference/kotlin/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 |
|---|---|
| `context: https://developer.android.com/reference/android/content/Context.html!` | The context that will launch the new task stack or generate a PendingIntent |

| Returns |
|---|---|
| `https://developer.android.com/reference/kotlin/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)

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

> [!CAUTION]
> **This function 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/kotlin/androidx/core/app/TaskStackBuilder#addParentStack(android.app.Activity)`.

| Parameters |
|---|---|
| `index: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | 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)

```
fun getIntentCount(): Int
```

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | the number of intents added so far. |

### getIntents

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

```
fun getIntents(): Array<Intent!>
```

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://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-array/index.html<https://developer.android.com/reference/android/content/Intent.html!>` | 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)

```
fun getPendingIntent(requestCode: Int, flags: Int): PendingIntent?
```

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

| Parameters |
|---|---|
| `requestCode: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | Private request code for the sender |
| `flags: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | 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://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)

```
fun getPendingIntent(requestCode: Int, flags: Int, isMutable: Boolean): PendingIntent?
```

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/kotlin/androidx/core/app/TaskStackBuilder#getPendingIntent(int,int)`.

### getPendingIntent

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

```
fun getPendingIntent(requestCode: Int, flags: Int, options: Bundle?): PendingIntent?
```

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

| Parameters |
|---|---|
| `requestCode: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | Private request code for the sender |
| `flags: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | 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. |
| `options: https://developer.android.com/reference/android/os/Bundle.html?` | 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://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)

```
fun getPendingIntent(
    requestCode: Int,
    flags: Int,
    options: Bundle?,
    isMutable: Boolean
): PendingIntent?
```

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/kotlin/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)

```
fun [iterator](https://developer.android.com/reference/kotlin/androidx/core/app/TaskStackBuilder#iterator())(): (Mutable)Iterator<Intent!>
```

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

### startActivities

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

```
fun startActivities(): Unit
```

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)

```
fun startActivities(options: Bundle?): Unit
```

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 |
|---|---|
| `options: https://developer.android.com/reference/android/os/Bundle.html?` | 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)` |