ActivityCompat.java revision c9cf2eb0a9b6694d0fda3dbc313844955db60820
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;
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 {
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    /**
69     * Start a set of activities as a synthesized task stack, if able.
70     *
71     * <p>In API level 11 (Android 3.0/Honeycomb) the recommended conventions for
72     * app navigation using the back key changed. The back key's behavior is local
73     * to the current task and does not capture navigation across different tasks.
74     * Navigating across tasks and easily reaching the previous task is accomplished
75     * through the "recents" UI, accessible through the software-provided Recents key
76     * on the navigation or system bar. On devices with the older hardware button configuration
77     * the recents UI can be accessed with a long press on the Home key.</p>
78     *
79     * <p>When crossing from one task stack to another post-Android 3.0,
80     * the application should synthesize a back stack/history for the new task so that
81     * the user may navigate out of the new task and back to the Launcher by repeated
82     * presses of the back key. Back key presses should not navigate across task stacks.</p>
83     *
84     * <p>startActivities provides a mechanism for constructing a synthetic task stack of
85     * multiple activities. If the underlying API is not available on the system this method
86     * will return false.</p>
87     *
88     * @param activity Start activities using this activity as the starting context
89     * @param intents Array of intents defining the activities that will be started. The element
90     *                length-1 will correspond to the top activity on the resulting task stack.
91     * @return true if the underlying API was available and the call was successful, false otherwise
92     */
93    public static boolean startActivities(Activity activity, Intent[] intents) {
94        if (Build.VERSION.SDK_INT >= 11) {
95            ActivityCompatHoneycomb.startActivities(activity, intents);
96            return true;
97        }
98        return false;
99    }
100}
101