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