ActivityCompat.java revision 3a96487b54eca412f51ad00b8f8096055e94dcbb
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.v4.content.ContextCompat;
24
25/**
26 * Helper for accessing features in {@link android.app.Activity}
27 * introduced after API level 4 in a backwards compatible fashion.
28 */
29public class ActivityCompat extends ContextCompat {
30    /**
31     * Invalidate the activity's options menu, if able.
32     *
33     * <p>Before API level 11 (Android 3.0/Honeycomb) the lifecycle of the
34     * options menu was controlled primarily by the user's operation of
35     * the hardware menu key. When the user presses down on the menu key
36     * for the first time the menu was created and prepared by calls
37     * to {@link Activity#onCreateOptionsMenu(android.view.Menu)} and
38     * {@link Activity#onPrepareOptionsMenu(android.view.Menu)} respectively.
39     * Subsequent presses of the menu key kept the existing instance of the
40     * Menu itself and called {@link Activity#onPrepareOptionsMenu(android.view.Menu)}
41     * to give the activity an opportunity to contextually alter the menu
42     * before the menu panel was shown.</p>
43     *
44     * <p>In Android 3.0+ the Action Bar forces the options menu to be built early
45     * so that items chosen to show as actions may be displayed when the activity
46     * first becomes visible. The Activity method invalidateOptionsMenu forces
47     * the entire menu to be destroyed and recreated from
48     * {@link Activity#onCreateOptionsMenu(android.view.Menu)}, offering a similar
49     * though heavier-weight opportunity to change the menu's contents. Normally
50     * this functionality is used to support a changing configuration of Fragments.</p>
51     *
52     * <p>Applications may use this support helper to signal a significant change in
53     * activity state that should cause the options menu to be rebuilt. If the app
54     * is running on an older platform version that does not support menu invalidation
55     * the app will still receive {@link Activity#onPrepareOptionsMenu(android.view.Menu)}
56     * the next time the user presses the menu key and this method will return false.
57     * If this method returns true the options menu was successfully invalidated.</p>
58     *
59     * @param activity Invalidate the options menu of this activity
60     * @return true if this operation was supported and it completed; false if it was not available.
61     */
62    public static boolean invalidateOptionsMenu(Activity activity) {
63        if (Build.VERSION.SDK_INT >= 11) {
64            ActivityCompatHoneycomb.invalidateOptionsMenu(activity);
65            return true;
66        }
67        return false;
68    }
69
70    /**
71     * Start an activity with additional launch information, if able.
72     *
73     * <p>In Android 4.1+ additional options were introduced to allow for more
74     * control on activity launch animations. Applications can use this method
75     * along with {@link ActivityOptionsCompat} to use these animations when
76     * available. When run on versions of the platform where this feature does
77     * not exist the activity will be launched normally.</p>
78     *
79     * @param activity Context to launch activity from.
80     * @param intent The description of the activity to start.
81     * @param options Additional options for how the Activity should be started.
82     *                May be null if there are no options. See
83     *                {@link ActivityOptionsCompat} for how to build the Bundle
84     *                supplied here; there are no supported definitions for
85     *                building it manually.
86     */
87    public static void startActivity(Activity activity, Intent intent, Bundle options) {
88        if (Build.VERSION.SDK_INT >= 16) {
89            ActivityCompatJB.startActivity(activity, intent, options);
90        } else {
91            activity.startActivity(intent);
92        }
93    }
94
95    /**
96     * Start new activity with options, if able, for which you would like a
97     * result when it finished.
98     *
99     * <p>In Android 4.1+ additional options were introduced to allow for more
100     * control on activity launch animations. Applications can use this method
101     * along with {@link ActivityOptionsCompat} to use these animations when
102     * available. When run on versions of the platform where this feature does
103     * not exist the activity will be launched normally.</p>
104     *
105     * @param activity Origin activity to launch from.
106     * @param intent The description of the activity to start.
107     * @param requestCode If >= 0, this code will be returned in
108     *                   onActivityResult() when the activity exits.
109     * @param options Additional options for how the Activity should be started.
110     *                May be null if there are no options. See
111     *                {@link ActivityOptionsCompat} for how to build the Bundle
112     *                supplied here; there are no supported definitions for
113     *                building it manually.
114     */
115    public static void startActivityForResult(Activity activity, Intent intent, int requestCode, Bundle options) {
116        if (Build.VERSION.SDK_INT >= 16) {
117            ActivityCompatJB.startActivityForResult(activity, intent, requestCode, options);
118        } else {
119            activity.startActivityForResult(intent, requestCode);
120        }
121    }
122}
123