ActivityCompat.java revision 559b5e8554651ffc9f9cc639f8e363b9494fc98a
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; 23import android.support.annotation.Nullable; 24import android.support.v4.content.ContextCompat; 25import android.view.View; 26 27import java.util.List; 28import java.util.Map; 29 30/** 31 * Helper for accessing features in {@link android.app.Activity} 32 * introduced after API level 4 in a backwards compatible fashion. 33 */ 34public class ActivityCompat extends ContextCompat { 35 /** 36 * Invalidate the activity's options menu, if able. 37 * 38 * <p>Before API level 11 (Android 3.0/Honeycomb) the lifecycle of the 39 * options menu was controlled primarily by the user's operation of 40 * the hardware menu key. When the user presses down on the menu key 41 * for the first time the menu was created and prepared by calls 42 * to {@link Activity#onCreateOptionsMenu(android.view.Menu)} and 43 * {@link Activity#onPrepareOptionsMenu(android.view.Menu)} respectively. 44 * Subsequent presses of the menu key kept the existing instance of the 45 * Menu itself and called {@link Activity#onPrepareOptionsMenu(android.view.Menu)} 46 * to give the activity an opportunity to contextually alter the menu 47 * before the menu panel was shown.</p> 48 * 49 * <p>In Android 3.0+ the Action Bar forces the options menu to be built early 50 * so that items chosen to show as actions may be displayed when the activity 51 * first becomes visible. The Activity method invalidateOptionsMenu forces 52 * the entire menu to be destroyed and recreated from 53 * {@link Activity#onCreateOptionsMenu(android.view.Menu)}, offering a similar 54 * though heavier-weight opportunity to change the menu's contents. Normally 55 * this functionality is used to support a changing configuration of Fragments.</p> 56 * 57 * <p>Applications may use this support helper to signal a significant change in 58 * activity state that should cause the options menu to be rebuilt. If the app 59 * is running on an older platform version that does not support menu invalidation 60 * the app will still receive {@link Activity#onPrepareOptionsMenu(android.view.Menu)} 61 * the next time the user presses the menu key and this method will return false. 62 * If this method returns true the options menu was successfully invalidated.</p> 63 * 64 * @param activity Invalidate the options menu of this activity 65 * @return true if this operation was supported and it completed; false if it was not available. 66 */ 67 public static boolean invalidateOptionsMenu(Activity activity) { 68 if (Build.VERSION.SDK_INT >= 11) { 69 ActivityCompatHoneycomb.invalidateOptionsMenu(activity); 70 return true; 71 } 72 return false; 73 } 74 75 /** 76 * Start an activity with additional launch information, if able. 77 * 78 * <p>In Android 4.1+ additional options were introduced to allow for more 79 * control on activity launch animations. Applications can use this method 80 * along with {@link ActivityOptionsCompat} to use these animations when 81 * available. When run on versions of the platform where this feature does 82 * not exist the activity will be launched normally.</p> 83 * 84 * @param activity Context to launch activity from. 85 * @param intent The description of the activity to start. 86 * @param options Additional options for how the Activity should be started. 87 * May be null if there are no options. See 88 * {@link ActivityOptionsCompat} for how to build the Bundle 89 * supplied here; there are no supported definitions for 90 * building it manually. 91 */ 92 public static void startActivity(Activity activity, Intent intent, @Nullable Bundle options) { 93 if (Build.VERSION.SDK_INT >= 16) { 94 ActivityCompatJB.startActivity(activity, intent, options); 95 } else { 96 activity.startActivity(intent); 97 } 98 } 99 100 /** 101 * Start new activity with options, if able, for which you would like a 102 * result when it finished. 103 * 104 * <p>In Android 4.1+ additional options were introduced to allow for more 105 * control on activity launch animations. Applications can use this method 106 * along with {@link ActivityOptionsCompat} to use these animations when 107 * available. When run on versions of the platform where this feature does 108 * not exist the activity will be launched normally.</p> 109 * 110 * @param activity Origin activity to launch from. 111 * @param intent The description of the activity to start. 112 * @param requestCode If >= 0, this code will be returned in 113 * onActivityResult() when the activity exits. 114 * @param options Additional options for how the Activity should be started. 115 * May be null if there are no options. See 116 * {@link ActivityOptionsCompat} for how to build the Bundle 117 * supplied here; there are no supported definitions for 118 * building it manually. 119 */ 120 public static void startActivityForResult(Activity activity, Intent intent, int requestCode, 121 @Nullable Bundle options) { 122 if (Build.VERSION.SDK_INT >= 16) { 123 ActivityCompatJB.startActivityForResult(activity, intent, requestCode, options); 124 } else { 125 activity.startActivityForResult(intent, requestCode); 126 } 127 } 128 129 /** 130 * Finish this activity, and tries to finish all activities immediately below it 131 * in the current task that have the same affinity. 132 * 133 * <p>On Android 4.1+ calling this method will call through to the native version of this 134 * method. For other platforms {@link Activity#finish()} will be called instead.</p> 135 */ 136 public static void finishAffinity(Activity activity) { 137 if (Build.VERSION.SDK_INT >= 16) { 138 ActivityCompatJB.finishAffinity(activity); 139 } else { 140 activity.finish(); 141 } 142 } 143 144 /** 145 * Reverses the Activity Scene entry Transition and triggers the calling Activity 146 * to reverse its exit Transition. When the exit Transition completes, 147 * {@link Activity#finish()} is called. If no entry Transition was used, finish() is called 148 * immediately and the Activity exit Transition is run. 149 * 150 * <p>On Android 4.4 or lower, this method only finishes the Activity with no 151 * special exit transition.</p> 152 */ 153 public static void finishAfterTransition(Activity activity) { 154 if (Build.VERSION.SDK_INT >= 21 || Build.VERSION.CODENAME.equals("L")) { 155 ActivityCompat21.finishAfterTransition(activity); 156 } else { 157 activity.finish(); 158 } 159 } 160 161 /** 162 * When {@link android.app.ActivityOptions#makeSceneTransitionAnimation(Activity, 163 * android.view.View, String)} was used to start an Activity, <var>listener</var> 164 * will be called to handle shared elements on the <i>launched</i> Activity. This requires 165 * {@link android.view.Window#FEATURE_CONTENT_TRANSITIONS}. 166 * 167 * @param listener Used to manipulate shared element transitions on the launched Activity. 168 */ 169 public static void setEnterSharedElementListener(Activity activity, 170 SharedElementListener listener) { 171 if (Build.VERSION.SDK_INT >= 21 || Build.VERSION.CODENAME.equals("L")) { 172 ActivityCompat21.setEnterSharedElementListener(activity, createListener(listener)); 173 } 174 } 175 176 /** 177 * When {@link android.app.ActivityOptions#makeSceneTransitionAnimation(Activity, 178 * android.view.View, String)} was used to start an Activity, <var>listener</var> 179 * will be called to handle shared elements on the <i>launching</i> Activity. Most 180 * calls will only come when returning from the started Activity. 181 * This requires {@link android.view.Window#FEATURE_CONTENT_TRANSITIONS}. 182 * 183 * @param listener Used to manipulate shared element transitions on the launching Activity. 184 */ 185 public static void setExitSharedElementListener(Activity activity, 186 SharedElementListener listener) { 187 if (Build.VERSION.SDK_INT >= 21 || Build.VERSION.CODENAME.equals("L")) { 188 ActivityCompat21.setExitSharedElementListener(activity, createListener(listener)); 189 } 190 } 191 192 private static ActivityCompat21.SharedElementListener21 createListener( 193 SharedElementListener listener) { 194 ActivityCompat21.SharedElementListener21 newListener = null; 195 if (listener != null) { 196 newListener = new SharedElementListener21Impl(listener); 197 } 198 return newListener; 199 } 200 201 private static class SharedElementListener21Impl 202 extends ActivityCompat21.SharedElementListener21 { 203 private SharedElementListener mListener; 204 205 public SharedElementListener21Impl(SharedElementListener listener) { 206 mListener = listener; 207 } 208 209 @Override 210 public void setSharedElementStart(List<String> sharedElementNames, 211 List<View> sharedElements, List<View> sharedElementSnapshots) { 212 mListener.setSharedElementStart(sharedElementNames, sharedElements, 213 sharedElementSnapshots); 214 } 215 216 @Override 217 public void setSharedElementEnd(List<String> sharedElementNames, List<View> sharedElements, 218 List<View> sharedElementSnapshots) { 219 mListener.setSharedElementEnd(sharedElementNames, sharedElements, 220 sharedElementSnapshots); 221 } 222 223 @Override 224 public void handleRejectedSharedElements(List<View> rejectedSharedElements) { 225 mListener.handleRejectedSharedElements(rejectedSharedElements); 226 } 227 228 @Override 229 public void remapSharedElements(List<String> names, Map<String, View> sharedElements) { 230 mListener.remapSharedElements(names, sharedElements); 231 } 232 } 233} 234