/* * Copyright (C) 2012 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package android.support.v4.app; import android.app.Activity; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager.NameNotFoundException; import android.os.Build; import android.os.Bundle; import android.support.v4.content.ContextCompat; import android.support.v4.content.IntentCompat; import android.util.Log; import java.util.ArrayList; import java.util.Iterator; /** * 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 {@link #startActivities()} method or sending the * {@link PendingIntent} generated by {@link #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.
* *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.
*/ public void startActivities() { startActivities(null); } /** * 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.
* * @param options Additional options for how the Activity should be started. * See {@link android.content.Context#startActivity(Intent, Bundle) */ public void startActivities(Bundle options) { if (mIntents.isEmpty()) { throw new IllegalStateException( "No intents added to TaskStackBuilder; cannot startActivities"); } Intent[] intents = mIntents.toArray(new Intent[mIntents.size()]); intents[0].addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | IntentCompat.FLAG_ACTIVITY_CLEAR_TASK | IntentCompat.FLAG_ACTIVITY_TASK_ON_HOME); if (!ContextCompat.startActivities(mSourceContext, intents, options)) { Intent topIntent = intents[intents.length - 1]; topIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); mSourceContext.startActivity(topIntent); } } /** * Obtain a {@link PendingIntent} for launching the task constructed by this builder so far. * * @param requestCode Private request code for the sender * @param flags May be {@link PendingIntent#FLAG_ONE_SHOT}, * {@link PendingIntent#FLAG_NO_CREATE}, {@link PendingIntent#FLAG_CANCEL_CURRENT}, * {@link PendingIntent#FLAG_UPDATE_CURRENT}, or any of the flags supported by * {@link Intent#fillIn(Intent, int)} to control which unspecified parts of the * intent that can be supplied when the actual send happens. * @return The obtained PendingIntent */ public PendingIntent getPendingIntent(int requestCode, int flags) { return getPendingIntent(requestCode, flags, null); } /** * Obtain a {@link PendingIntent} for launching the task constructed by this builder so far. * * @param requestCode Private request code for the sender * @param flags May be {@link PendingIntent#FLAG_ONE_SHOT}, * {@link PendingIntent#FLAG_NO_CREATE}, {@link PendingIntent#FLAG_CANCEL_CURRENT}, * {@link PendingIntent#FLAG_UPDATE_CURRENT}, or any of the flags supported by * {@link Intent#fillIn(Intent, int)} to control which unspecified parts of the * intent that can be supplied when the actual send happens. * @param options Additional options for how the Activity should be started. * See {@link android.content.Context#startActivity(Intent, Bundle) * @return The obtained PendingIntent */ public PendingIntent getPendingIntent(int requestCode, int flags, Bundle options) { if (mIntents.isEmpty()) { throw new IllegalStateException( "No intents added to TaskStackBuilder; cannot getPendingIntent"); } Intent[] intents = mIntents.toArray(new Intent[mIntents.size()]); intents[0].addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | IntentCompat.FLAG_ACTIVITY_CLEAR_TASK | IntentCompat.FLAG_ACTIVITY_TASK_ON_HOME); return IMPL.getPendingIntent(mSourceContext, intents, requestCode, flags, options); } /** * 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. * * @return An array containing the intents added to this builder. */ public Intent[] getIntents() { return mIntents.toArray(new Intent[mIntents.size()]); } }