ActivityCompat

public class ActivityCompat
extends ContextCompat

java.lang.Object
   ↳ androidx.core.content.ContextCompat
     ↳ androidx.core.app.ActivityCompat


Helper for accessing features in Activity.

Summary

Nested classes

interface ActivityCompat.OnRequestPermissionsResultCallback

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

interface ActivityCompat.PermissionCompatDelegate

Customizable delegate that allows delegating permission compatibility methods to a custom implementation. 

Protected constructors

ActivityCompat()

This class should not be instantiated, but the constructor must be visible for the class to be extended (as in support-v13).

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.

static Uri getReferrer(Activity activity)

Return information about who launched this activity.

static boolean invalidateOptionsMenu(Activity activity)

This method is deprecated. Use Activity.invalidateOptionsMenu() directly.

static boolean isLaunchedFromBubble(Activity activity)

Indicates whether this activity is launched from a bubble.

static void postponeEnterTransition(Activity activity)
static void recreate(Activity activity)

Cause the given Activity to be recreated with a new instance.

static DragAndDropPermissionsCompat requestDragAndDropPermissions(Activity activity, DragEvent dragEvent)

Create DragAndDropPermissionsCompat object bound to this activity and controlling the access permissions for content URIs associated with the DragEvent.

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

Requests permissions to be granted to this application.

static <T extends View> T requireViewById(Activity activity, int id)

Finds a view that was identified by the android:id XML attribute that was processed in Activity.onCreate(Bundle), or throws an IllegalArgumentException if the ID is invalid, or there is no matching view in the hierarchy.

static void setEnterSharedElementCallback(Activity activity, SharedElementCallback callback)

When ActivityOptions.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 ActivityOptions.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 void setLocusContext(Activity activity, LocusIdCompat locusId, Bundle bundle)

Sets the LocusIdCompat for this activity.

static void setPermissionCompatDelegate(ActivityCompat.PermissionCompatDelegate delegate)

Sets the permission delegate for ActivityCompat.

static boolean shouldShowRequestPermissionRationale(Activity activity, String permission)

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

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 startIntentSenderForResult(Activity activity, IntentSender intent, int requestCode, Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options)

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

static void startPostponedEnterTransition(Activity activity)

Inherited methods

Protected constructors

ActivityCompat

protected ActivityCompat ()

This class should not be instantiated, but the constructor must be visible for the class to be extended (as in support-v13).

Public methods

finishAffinity

public 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.

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

Parameters
activity Activity

finishAfterTransition

public static 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, Activity.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

public static Uri getReferrer (Activity activity)

Return information about who launched this activity. If the launching Intent contains an Intent.EXTRA_REFERRER, that will be returned as-is; otherwise, if known, an android-app: referrer URI containing the package name that started the Intent will be returned. This may return null if no referrer can be identified -- it is neither explicitly specified, nor is it known which application package was involved.

If called while inside the handling of Activity.onNewIntent(Intent), this function will return the referrer that submitted that new intent to the activity. Otherwise, it always returns the referrer of the original Intent.

Note that this is not a security feature -- you can not trust the referrer information, applications can spoof it.

Parameters
activity Activity

Returns
Uri

invalidateOptionsMenu

public static boolean invalidateOptionsMenu (Activity activity)

This method is deprecated.
Use Activity.invalidateOptionsMenu() directly.

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 Activity.onCreateOptionsMenu(android.view.Menu) and Activity.onPrepareOptionsMenu(android.view.Menu) respectively. Subsequent presses of the menu key kept the existing instance of the Menu itself and called Activity.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 Activity.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 Activity.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.

isLaunchedFromBubble

public static boolean isLaunchedFromBubble (Activity activity)

Indicates whether this activity is launched from a bubble. A bubble is a floating shortcut on the screen that expands to show an activity. If your activity can be used normally or as a bubble, you might use this method to check if the activity is bubbled to modify any behaviour that might be different between the normal activity and the bubbled activity. For example, if you normally cancel the notification associated with the activity when you open the activity, you might not want to do that when you're bubbled as that would remove the bubble.

Parameters
activity Activity

Returns
boolean true if the activity is launched from a bubble.

postponeEnterTransition

public static void postponeEnterTransition (Activity activity)

Parameters
activity Activity

recreate

public static void recreate (Activity activity)

Cause the given Activity to be recreated with a new instance. This version of the method allows a consistent behavior across API levels, emulating what happens on Android Pie (and newer) when running on older platforms.

Parameters
activity Activity: The activity to recreate

requestDragAndDropPermissions

public static DragAndDropPermissionsCompat requestDragAndDropPermissions (Activity activity, 
                DragEvent dragEvent)

Create DragAndDropPermissionsCompat object bound to this activity and controlling the access permissions for content URIs associated with the DragEvent.

Parameters
activity Activity

dragEvent DragEvent: Drag event to request permission for

Returns
DragAndDropPermissionsCompat The DragAndDropPermissionsCompat object used to control access to the content URIs. null if no content URIs are associated with the event or if permissions could not be granted.

requestPermissions

public static 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 dangerous, regardless whether they are declared by the platform or a third-party app.

Normal permissions PermissionInfo.PROTECTION_NORMAL are granted at install time if requested in the manifest. Signature permissions PermissionInfo.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.

Call shouldShowRequestPermissionRationale(Activity, String) before calling this API to check if the system recommends to show a rationale dialog before asking for a permission.

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 ActivityCompat.OnRequestPermissionsResultCallback.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