ActivityCompat.java revision 0306ebc908d5dcf148aa319a9734b419eacb23b5
1/* 2 * Copyright (C) 2011 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package android.support.v4.app; 18 19import android.app.Activity; 20import android.content.Intent; 21import android.os.Build; 22import android.os.Bundle; 23 24/** 25 * Helper for accessing features in {@link android.app.Activity} 26 * introduced after API level 4 in a backwards compatible fashion. 27 */ 28public class ActivityCompat { 29 /** 30 * Invalidate the activity's options menu, if able. 31 * 32 * <p>Before API level 11 (Android 3.0/Honeycomb) the lifecycle of the 33 * options menu was controlled primarily by the user's operation of 34 * the hardware menu key. When the user presses down on the menu key 35 * for the first time the menu was created and prepared by calls 36 * to {@link Activity#onCreateOptionsMenu(android.view.Menu)} and 37 * {@link Activity#onPrepareOptionsMenu(android.view.Menu)} respectively. 38 * Subsequent presses of the menu key kept the existing instance of the 39 * Menu itself and called {@link Activity#onPrepareOptionsMenu(android.view.Menu)} 40 * to give the activity an opportunity to contextually alter the menu 41 * before the menu panel was shown.</p> 42 * 43 * <p>In Android 3.0+ the Action Bar forces the options menu to be built early 44 * so that items chosen to show as actions may be displayed when the activity 45 * first becomes visible. The Activity method invalidateOptionsMenu forces 46 * the entire menu to be destroyed and recreated from 47 * {@link Activity#onCreateOptionsMenu(android.view.Menu)}, offering a similar 48 * though heavier-weight opportunity to change the menu's contents. Normally 49 * this functionality is used to support a changing configuration of Fragments.</p> 50 * 51 * <p>Applications may use this support helper to signal a significant change in 52 * activity state that should cause the options menu to be rebuilt. If the app 53 * is running on an older platform version that does not support menu invalidation 54 * the app will still receive {@link Activity#onPrepareOptionsMenu(android.view.Menu)} 55 * the next time the user presses the menu key and this method will return false. 56 * If this method returns true the options menu was successfully invalidated.</p> 57 * 58 * @param activity Invalidate the options menu of this activity 59 * @return true if this operation was supported and it completed; false if it was not available. 60 */ 61 public static boolean invalidateOptionsMenu(Activity activity) { 62 if (Build.VERSION.SDK_INT >= 11) { 63 ActivityCompatHoneycomb.invalidateOptionsMenu(activity); 64 return true; 65 } 66 return false; 67 } 68 69 /** 70 * Start a set of activities as a synthesized task stack, if able. 71 * 72 * <p>In API level 11 (Android 3.0/Honeycomb) the recommended conventions for 73 * app navigation using the back key changed. The back key's behavior is local 74 * to the current task and does not capture navigation across different tasks. 75 * Navigating across tasks and easily reaching the previous task is accomplished 76 * through the "recents" UI, accessible through the software-provided Recents key 77 * on the navigation or system bar. On devices with the older hardware button configuration 78 * the recents UI can be accessed with a long press on the Home key.</p> 79 * 80 * <p>When crossing from one task stack to another post-Android 3.0, 81 * the application should synthesize a back stack/history for the new task so that 82 * the user may navigate out of the new task and back to the Launcher by repeated 83 * presses of the back key. Back key presses should not navigate across task stacks.</p> 84 * 85 * <p>startActivities provides a mechanism for constructing a synthetic task stack of 86 * multiple activities. If the underlying API is not available on the system this method 87 * will return false.</p> 88 * 89 * @param activity Start activities using this activity as the starting context 90 * @param intents Array of intents defining the activities that will be started. The element 91 * length-1 will correspond to the top activity on the resulting task stack. 92 * @return true if the underlying API was available and the call was successful, false otherwise 93 */ 94 public static boolean startActivities(Activity activity, Intent[] intents) { 95 return startActivities(activity, intents, null); 96 } 97 98 /** 99 * Start a set of activities as a synthesized task stack, if able. 100 * 101 * <p>In API level 11 (Android 3.0/Honeycomb) the recommended conventions for 102 * app navigation using the back key changed. The back key's behavior is local 103 * to the current task and does not capture navigation across different tasks. 104 * Navigating across tasks and easily reaching the previous task is accomplished 105 * through the "recents" UI, accessible through the software-provided Recents key 106 * on the navigation or system bar. On devices with the older hardware button configuration 107 * the recents UI can be accessed with a long press on the Home key.</p> 108 * 109 * <p>When crossing from one task stack to another post-Android 3.0, 110 * the application should synthesize a back stack/history for the new task so that 111 * the user may navigate out of the new task and back to the Launcher by repeated 112 * presses of the back key. Back key presses should not navigate across task stacks.</p> 113 * 114 * <p>startActivities provides a mechanism for constructing a synthetic task stack of 115 * multiple activities. If the underlying API is not available on the system this method 116 * will return false.</p> 117 * 118 * @param activity Start activities using this activity as the starting context 119 * @param intents Array of intents defining the activities that will be started. The element 120 * length-1 will correspond to the top activity on the resulting task stack. 121 * @param options Additional options for how the Activity should be started. 122 * See {@link android.content.Context#startActivity(Intent, Bundle) 123 * @return true if the underlying API was available and the call was successful, false otherwise 124 */ 125 public static boolean startActivities(Activity activity, Intent[] intents, 126 Bundle options) { 127 final int version = Build.VERSION.SDK_INT; 128 if (version >= 16) { 129 ActivityCompatJellybean.startActivities(activity, intents, options); 130 return true; 131 } else if (version >= 11) { 132 ActivityCompatHoneycomb.startActivities(activity, intents); 133 return true; 134 } 135 return false; 136 } 137} 138