1ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell/* 2ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell * Copyright (C) 2011 The Android Open Source Project 3ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell * 4ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell * Licensed under the Apache License, Version 2.0 (the "License"); 5ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell * you may not use this file except in compliance with the License. 6ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell * You may obtain a copy of the License at 7ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell * 8ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell * http://www.apache.org/licenses/LICENSE-2.0 9ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell * 10ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell * Unless required by applicable law or agreed to in writing, software 11ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell * distributed under the License is distributed on an "AS IS" BASIS, 12ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell * See the License for the specific language governing permissions and 14ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell * limitations under the License. 15ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell */ 16ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell 17ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powellpackage android.support.v4.app; 18ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell 19ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powellimport android.app.Activity; 20ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powellimport android.os.Build; 21575e098da5bc16ff8b95ca080284253fd206fe12Adam Powellimport android.support.v4.content.ContextCompat; 22ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell 23ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell/** 24ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell * Helper for accessing features in {@link android.app.Activity} 25ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell * introduced after API level 4 in a backwards compatible fashion. 26ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell */ 27575e098da5bc16ff8b95ca080284253fd206fe12Adam Powellpublic class ActivityCompat extends ContextCompat { 28ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell /** 29ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell * Invalidate the activity's options menu, if able. 30ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell * 31ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell * <p>Before API level 11 (Android 3.0/Honeycomb) the lifecycle of the 32ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell * options menu was controlled primarily by the user's operation of 33ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell * the hardware menu key. When the user presses down on the menu key 34ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell * for the first time the menu was created and prepared by calls 35ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell * to {@link Activity#onCreateOptionsMenu(android.view.Menu)} and 36ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell * {@link Activity#onPrepareOptionsMenu(android.view.Menu)} respectively. 37ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell * Subsequent presses of the menu key kept the existing instance of the 38ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell * Menu itself and called {@link Activity#onPrepareOptionsMenu(android.view.Menu)} 39ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell * to give the activity an opportunity to contextually alter the menu 40ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell * before the menu panel was shown.</p> 41ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell * 42ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell * <p>In Android 3.0+ the Action Bar forces the options menu to be built early 43ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell * so that items chosen to show as actions may be displayed when the activity 44ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell * first becomes visible. The Activity method invalidateOptionsMenu forces 45ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell * the entire menu to be destroyed and recreated from 46ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell * {@link Activity#onCreateOptionsMenu(android.view.Menu)}, offering a similar 47ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell * though heavier-weight opportunity to change the menu's contents. Normally 48ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell * this functionality is used to support a changing configuration of Fragments.</p> 49ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell * 50ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell * <p>Applications may use this support helper to signal a significant change in 51ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell * activity state that should cause the options menu to be rebuilt. If the app 52ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell * is running on an older platform version that does not support menu invalidation 53ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell * the app will still receive {@link Activity#onPrepareOptionsMenu(android.view.Menu)} 54ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell * the next time the user presses the menu key and this method will return false. 55ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell * If this method returns true the options menu was successfully invalidated.</p> 56ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell * 57ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell * @param activity Invalidate the options menu of this activity 58ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell * @return true if this operation was supported and it completed; false if it was not available. 59ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell */ 60ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell public static boolean invalidateOptionsMenu(Activity activity) { 61ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell if (Build.VERSION.SDK_INT >= 11) { 62ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell ActivityCompatHoneycomb.invalidateOptionsMenu(activity); 63ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell return true; 64ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell } 65ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell return false; 66ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell } 67ac4078687ff889e804b198bf5748e611bbb0fa30Adam Powell} 68