ActivityCompat.java revision 14ad84e85a8c09b016a621f0f0017594e0d7864b
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.os.Build; 21 22/** 23 * Helper for accessing features in {@link android.app.Activity} 24 * introduced after API level 4 in a backwards compatible fashion. 25 */ 26public class ActivityCompat { 27 /** 28 * Invalidate the activity's options menu, if able. 29 * 30 * <p>Before API level 11 (Android 3.0/Honeycomb) the lifecycle of the 31 * options menu was controlled primarily by the user's operation of 32 * the hardware menu key. When the user presses down on the menu key 33 * for the first time the menu was created and prepared by calls 34 * to {@link Activity#onCreateOptionsMenu(android.view.Menu)} and 35 * {@link Activity#onPrepareOptionsMenu(android.view.Menu)} respectively. 36 * Subsequent presses of the menu key kept the existing instance of the 37 * Menu itself and called {@link Activity#onPrepareOptionsMenu(android.view.Menu)} 38 * to give the activity an opportunity to contextually alter the menu 39 * before the menu panel was shown.</p> 40 * 41 * <p>In Android 3.0+ the Action Bar forces the options menu to be built early 42 * so that items chosen to show as actions may be displayed when the activity 43 * first becomes visible. The Activity method invalidateOptionsMenu forces 44 * the entire menu to be destroyed and recreated from 45 * {@link Activity#onCreateOptionsMenu(android.view.Menu)}, offering a similar 46 * though heavier-weight opportunity to change the menu's contents. Normally 47 * this functionality is used to support a changing configuration of Fragments.</p> 48 * 49 * <p>Applications may use this support helper to signal a significant change in 50 * activity state that should cause the options menu to be rebuilt. If the app 51 * is running on an older platform version that does not support menu invalidation 52 * the app will still receive {@link Activity#onPrepareOptionsMenu(android.view.Menu)} 53 * the next time the user presses the menu key and this method will return false. 54 * If this method returns true the options menu was successfully invalidated.</p> 55 * 56 * @param activity Invalidate the options menu of this activity 57 * @return true if this operation was supported and it completed; false if it was not available. 58 */ 59 public static boolean invalidateOptionsMenu(Activity activity) { 60 if (Build.VERSION.SDK_INT >= 11) { 61 ActivityCompatHoneycomb.invalidateOptionsMenu(activity); 62 return true; 63 } 64 return false; 65 } 66} 67