TaskStackBuilder
public final class TaskStackBuilder implements Iterable
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 startActivities method or sending the PendingIntent generated by getPendingIntent 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.
Summary
Nested types |
|---|
public interface TaskStackBuilder.SupportParentable |
Public methods |
|
|---|---|
@NonNull TaskStackBuilder |
addNextIntent(@NonNull Intent nextIntent)Add a new Intent to the task stack. |
@NonNull TaskStackBuilder |
addNextIntentWithParentStack(@NonNull Intent nextIntent)Add a new Intent with the resolved chain of parents for the target activity to the task stack. |
@NonNull TaskStackBuilder |
addParentStack(@NonNull Activity sourceActivity)Add the activity parent chain as specified by manifest |
@NonNull TaskStackBuilder |
addParentStack(@NonNull Class<Object> sourceActivityClass)Add the activity parent chain as specified by manifest |
@NonNull TaskStackBuilder |
addParentStack(@NonNull ComponentName sourceActivityName)Add the activity parent chain as specified by manifest |
static @NonNull TaskStackBuilder |
Return a new TaskStackBuilder for launching a fresh task stack consisting of a series of activities. |
@Nullable Intent |
editIntentAt(int index)Return the intent at the specified index for modification. |
static TaskStackBuilder |
This method is deprecated. use |
Intent |
This method is deprecated. Renamed to editIntentAt to better reflect intended usage |
int |
|
@NonNull Intent[] |
Return an array containing the intents added to this builder. |
@Nullable PendingIntent |
getPendingIntent(int requestCode, int flags)Obtain a |
@Nullable PendingIntent |
getPendingIntent(int requestCode, int flags, boolean isMutable)Obtains a |
@Nullable PendingIntent |
getPendingIntent(int requestCode, int flags, @Nullable Bundle options)Obtain a |
@Nullable PendingIntent |
getPendingIntent(Obtains a |
@NonNull Iterator<Intent> |
This method is deprecated. Use editIntentAt instead |
void |
Start the task stack constructed by this builder. |
void |
startActivities(@Nullable Bundle options)Start the task stack constructed by this builder. |
Inherited methods |
||||
|---|---|---|---|---|
|
Public methods
addNextIntent
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.
| Returns | |
|---|---|
@NonNull TaskStackBuilder |
This TaskStackBuilder for method chaining |
addNextIntentWithParentStack
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 addParentStack with the resolved ComponentName of nextIntent (if it can be resolved), followed by addNextIntent with nextIntent.
| Parameters | |
|---|---|
@NonNull Intent nextIntent |
Intent for the topmost Activity in the synthesized task stack. Its chain of parents as specified in the manifest will be added. |
| Returns | |
|---|---|
@NonNull TaskStackBuilder |
This TaskStackBuilder for method chaining. |
addParentStack
public @NonNull TaskStackBuilder addParentStack(@NonNull Activity sourceActivity)
Add the activity parent chain as specified by manifest
| Returns | |
|---|---|
@NonNull TaskStackBuilder |
This TaskStackBuilder for method chaining |
addParentStack
public @NonNull TaskStackBuilder addParentStack(@NonNull Class<Object> sourceActivityClass)
Add the activity parent chain as specified by manifest
| Returns | |
|---|---|
@NonNull TaskStackBuilder |
This TaskStackBuilder for method chaining |
addParentStack
public @NonNull TaskStackBuilder addParentStack(@NonNull ComponentName sourceActivityName)
Add the activity parent chain as specified by manifest
| Parameters | |
|---|---|
@NonNull ComponentName sourceActivityName |
Must specify an Activity component. All parents of this activity will be added |
| Returns | |
|---|---|
@NonNull TaskStackBuilder |
This TaskStackBuilder for method chaining |
create
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 | |
|---|---|
@NonNull Context context |
The context that will launch the new task stack or generate a PendingIntent |
| Returns | |
|---|---|
@NonNull TaskStackBuilder |
A new TaskStackBuilder |
editIntentAt
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 addParentStack.
| Parameters | |
|---|---|
int index |
Index from 0-getIntentCount() |
public static TaskStackBuilderfrom(Context context)
Return a new TaskStackBuilder for launching a fresh task stack consisting of a series of activities.
| Parameters | |
|---|---|
Context context |
The context that will launch the new task stack or generate a PendingIntent |
| Returns | |
|---|---|
TaskStackBuilder |
A new TaskStackBuilder |
public IntentgetIntent(int index)
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 addParentStack.
| Parameters | |
|---|---|
int index |
Index from 0-getIntentCount() |
| Returns | |
|---|---|
Intent |
the intent at position index |
getIntentCount
public int getIntentCount()
| Returns | |
|---|---|
int |
the number of intents added so far. |
getIntents
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 | |
|---|---|
@NonNull Intent[] |
An array containing the intents added to this builder. |
getPendingIntent
public @Nullable PendingIntent getPendingIntent(int requestCode, int flags)
Obtain a PendingIntent for launching the task constructed by this builder so far.
| Parameters | |
|---|---|
int requestCode |
Private request code for the sender |
int flags |
May be |
| Returns | |
|---|---|
@Nullable PendingIntent |
The obtained PendingIntent. May return null only if |
getPendingIntent
public @Nullable PendingIntent getPendingIntent(int requestCode, int flags, boolean isMutable)
Obtains a PendingIntent 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 getPendingIntent.
getPendingIntent
public @Nullable PendingIntent getPendingIntent(int requestCode, int flags, @Nullable Bundle options)
Obtain a PendingIntent for launching the task constructed by this builder so far.
| Parameters | |
|---|---|
int requestCode |
Private request code for the sender |
int flags |
May be |
@Nullable Bundle options |
Additional options for how the Activity should be started. See |
| Returns | |
|---|---|
@Nullable PendingIntent |
The obtained PendingIntent. May return null only if |
getPendingIntent
public @Nullable PendingIntent getPendingIntent(
int requestCode,
int flags,
@Nullable Bundle options,
boolean isMutable
)
Obtains a PendingIntent 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 getPendingIntent.
startActivities
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
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 | |
|---|---|
@Nullable Bundle options |
Additional options for how the Activity should be started. See |