Skip to content

Most visited

Recently visited

navigation

ActivityCompat

public class ActivityCompat
extends ContextCompat

java.lang.Object
   ↳ android.support.v4.content.ContextCompat
     ↳ android.support.v4.app.ActivityCompat


Helper for accessing features in Activity introduced after API level 4 in a backwards compatible fashion.

Summary

Nested classes

interface ActivityCompat.OnRequestPermissionsResultCallback

This interface is the contract for receiving the results for permission requests. 

Public constructors

ActivityCompat()

Public methods

static void finishAffinity(Activity activity)

Finish this activity, and tries to finish all activities immediately below it in the current task that have the same affinity.

static void finishAfterTransition(Activity activity)

Reverses the Activity Scene entry Transition and triggers the calling Activity to reverse its exit Transition.

Uri getReferrer(Activity activity)

Backwards compatible implementation of Activity.getReferrer.

static boolean invalidateOptionsMenu(Activity activity)

Invalidate the activity's options menu, if able.

static void postponeEnterTransition(Activity activity)
static void requestPermissions(Activity activity, String[] permissions, int requestCode)

Requests permissions to be granted to this application.

static void setEnterSharedElementCallback(Activity activity, SharedElementCallback callback)

When makeSceneTransitionAnimation(Activity, android.view.View, String) was used to start an Activity, callback will be called to handle shared elements on the launched Activity.

static void setExitSharedElementCallback(Activity activity, SharedElementCallback callback)

When makeSceneTransitionAnimation(Activity, android.view.View, String) was used to start an Activity, callback will be called to handle shared elements on the launching Activity.

static boolean shouldShowRequestPermissionRationale(Activity activity, String permission)

Gets whether you should show UI with rationale for requesting a permission.

static void startActivity(Activity activity, Intent intent, Bundle options)

Start an activity with additional launch information, if able.

static void startActivityForResult(Activity activity, Intent intent, int requestCode, Bundle options)

Start new activity with options, if able, for which you would like a result when it finished.

static void startPostponedEnterTransition(Activity activity)

Inherited methods

From class android.support.v4.content.ContextCompat
From class java.lang.Object

Public constructors

ActivityCompat

ActivityCompat ()

Public methods

finishAffinity

void finishAffinity (Activity activity)

Finish this activity, and tries to finish all activities immediately below it in the current task that have the same affinity.

On Android 4.1+ calling this method will call through to the native version of this method. For other platforms finish() will be called instead.

Parameters
activity Activity

finishAfterTransition

void finishAfterTransition (Activity activity)

Reverses the Activity Scene entry Transition and triggers the calling Activity to reverse its exit Transition. When the exit Transition completes, finish() is called. If no entry Transition was used, finish() is called immediately and the Activity exit Transition is run.

On Android 4.4 or lower, this method only finishes the Activity with no special exit transition.

Parameters
activity Activity

getReferrer

Uri getReferrer (Activity activity)

Backwards compatible implementation of Activity.getReferrer. Uses the platform's implementation if available, otherwise only falls back to digging any explicitly specified referrer from the activity's intent.

Parameters
activity Activity
Returns
Uri

invalidateOptionsMenu

boolean invalidateOptionsMenu (Activity activity)

Invalidate the activity's options menu, if able.

Before API level 11 (Android 3.0/Honeycomb) the lifecycle of the options menu was controlled primarily by the user's operation of the hardware menu key. When the user presses down on the menu key for the first time the menu was created and prepared by calls to onCreateOptionsMenu(android.view.Menu) and onPrepareOptionsMenu(android.view.Menu) respectively. Subsequent presses of the menu key kept the existing instance of the Menu itself and called onPrepareOptionsMenu(android.view.Menu) to give the activity an opportunity to contextually alter the menu before the menu panel was shown.

In Android 3.0+ the Action Bar forces the options menu to be built early so that items chosen to show as actions may be displayed when the activity first becomes visible. The Activity method invalidateOptionsMenu forces the entire menu to be destroyed and recreated from onCreateOptionsMenu(android.view.Menu), offering a similar though heavier-weight opportunity to change the menu's contents. Normally this functionality is used to support a changing configuration of Fragments.

Applications may use this support helper to signal a significant change in activity state that should cause the options menu to be rebuilt. If the app is running on an older platform version that does not support menu invalidation the app will still receive onPrepareOptionsMenu(android.view.Menu) the next time the user presses the menu key and this method will return false. If this method returns true the options menu was successfully invalidated.

Parameters
activity Activity: Invalidate the options menu of this activity
Returns
boolean true if this operation was supported and it completed; false if it was not available.

postponeEnterTransition

void postponeEnterTransition (Activity activity)

Parameters
activity Activity

requestPermissions

void requestPermissions (Activity activity, 
                String[] permissions, 
                int requestCode)

Requests permissions to be granted to this application. These permissions must be requested in your manifest, they should not be granted to your app, and they should have protection level #PROTECTION_DANGEROUS dangerous, regardless whether they are declared by the platform or a third-party app.

Normal permissions PROTECTION_NORMAL are granted at install time if requested in the manifest. Signature permissions PROTECTION_SIGNATURE are granted at install time if requested in the manifest and the signature of your app matches the signature of the app declaring the permissions.

If your app does not have the requested permissions the user will be presented with UI for accepting them. After the user has accepted or rejected the requested permissions you will receive a callback reporting whether the permissions were granted or not. Your activity has to implement ActivityCompat.OnRequestPermissionsResultCallback and the results of permission requests will be delivered to its onRequestPermissionsResult(int, String[], int[]) method.

Note that requesting a permission does not guarantee it will be granted and your app should be able to run without having this permission.

This method may start an activity allowing the user to choose which permissions to grant and which to reject. Hence, you should be prepared that your activity may be paused and resumed. Further, granting some permissions may require a restart of you application. In such a case, the system will recreate the activity stack before delivering the result to your onRequestPermissionsResult( int, String[], int[]).

When checking whether you have a permission you should use checkSelfPermission(android.content.Context, String).

Parameters
activity Activity: The target activity.
permissions String: The requested permissions.
requestCode int: Application specific request code to match with a result reported to onRequestPermissionsResult(int, String[], int[]).

See also:

setEnterSharedElementCallback

void setEnterSharedElementCallback (Activity activity, 
                SharedElementCallback callback)

When makeSceneTransitionAnimation(Activity, android.view.View, String) was used to start an Activity, callback will be called to handle shared elements on the launched Activity. This requires FEATURE_CONTENT_TRANSITIONS.

Parameters
activity Activity
callback SharedElementCallback: Used to manipulate shared element transitions on the launched Activity.

setExitSharedElementCallback

void setExitSharedElementCallback (Activity activity, 
                SharedElementCallback callback)

When makeSceneTransitionAnimation(Activity, android.view.View, String) was used to start an Activity, callback will be called to handle shared elements on the launching Activity. Most calls will only come when returning from the started Activity. This requires FEATURE_CONTENT_TRANSITIONS.

Parameters
activity Activity
callback SharedElementCallback: Used to manipulate shared element transitions on the launching Activity.

shouldShowRequestPermissionRationale

boolean shouldShowRequestPermissionRationale (Activity activity, 
                String permission)

Gets whether you should show UI with rationale for requesting a permission. You should do this only if you do not have the permission and the context in which the permission is requested does not clearly communicate to the user what would be the benefit from granting this permission.

For example, if you write a camera app, requesting the camera permission would be expected by the user and no rationale for why it is requested is needed. If however, the app needs location for tagging photos then a non-tech savvy user may wonder how location is related to taking photos. In this case you may choose to show UI with rationale of requesting this permission.

Parameters
activity Activity: The target activity.
permission String: A permission your app wants to request.
Returns
boolean Whether you can show permission rationale UI.

See also:

startActivity

void startActivity (Activity activity, 
                Intent intent, 
                Bundle options)

Start an activity with additional launch information, if able.

In Android 4.1+ additional options were introduced to allow for more control on activity launch animations. Applications can use this method along with ActivityOptionsCompat to use these animations when available. When run on versions of the platform where this feature does not exist the activity will be launched normally.

Parameters
activity Activity: Context to launch activity from.
intent Intent: The description of the activity to start.
options Bundle: Additional options for how the Activity should be started. May be null if there are no options. See ActivityOptionsCompat for how to build the Bundle supplied here; there are no supported definitions for building it manually.

startActivityForResult

void startActivityForResult (Activity activity, 
                Intent intent, 
                int requestCode, 
                Bundle options)

Start new activity with options, if able, for which you would like a result when it finished.

In Android 4.1+ additional options were introduced to allow for more control on activity launch animations. Applications can use this method along with ActivityOptionsCompat to use these animations when available. When run on versions of the platform where this feature does not exist the activity will be launched normally.

Parameters
activity Activity: Origin activity to launch from.
intent Intent: The description of the activity to start.
requestCode int: If >= 0, this code will be returned in onActivityResult() when the activity exits.
options Bundle: Additional options for how the Activity should be started. May be null if there are no options. See ActivityOptionsCompat for how to build the Bundle supplied here; there are no supported definitions for building it manually.

startPostponedEnterTransition

void startPostponedEnterTransition (Activity activity)

Parameters
activity Activity
This site uses cookies to store your preferences for site-specific language and display options.

Hooray!

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.