ActivityCompat

open class ActivityCompat : ContextCompat
kotlin.Any
   ↳ androidx.core.content.ContextCompat
   ↳ androidx.core.app.ActivityCompat

Helper for accessing features in android.app.Activity.

Summary

Nested classes

abstract

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

abstract

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

Protected constructors

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

Public methods

open static Unit
finishAffinity(@NonNull activity: Activity)

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

open static Unit
finishAfterTransition(@NonNull activity: Activity)

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

open static Uri?
getReferrer(@NonNull activity: Activity)

Return information about who launched this activity.

open static Boolean

Invalidate the activity's options menu, if able.

open static Unit
postponeEnterTransition(@NonNull activity: Activity)

open static Unit
recreate(@NonNull activity: Activity)

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

open static DragAndDropPermissionsCompat?

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

open static Unit
requestPermissions(@NonNull activity: Activity, @NonNull permissions: Array<String!>, requestCode: Int)

Requests permissions to be granted to this application.

open static T
requireViewById(@NonNull activity: Activity, @IdRes id: Int)

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

open static Unit
setEnterSharedElementCallback(@NonNull activity: Activity, @Nullable callback: SharedElementCallback?)

When android.app.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.

open static Unit
setExitSharedElementCallback(@NonNull activity: Activity, @Nullable callback: SharedElementCallback?)

When android.app.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.

open static Unit

Sets the permission delegate for ActivityCompat.

open static Boolean
shouldShowRequestPermissionRationale(@NonNull activity: Activity, @NonNull permission: String)

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

open static Unit
startActivityForResult(@NonNull activity: Activity, @NonNull intent: Intent, requestCode: Int, @Nullable options: Bundle?)

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

open static Unit
startIntentSenderForResult(@NonNull activity: Activity, @NonNull intent: IntentSender, requestCode: Int, @Nullable fillInIntent: Intent?, flagsMask: Int, flagsValues: Int, extraFlags: Int, @Nullable options: Bundle?)

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

open static Unit

Inherited functions

Protected constructors

<init>

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

open static fun finishAffinity(@NonNull activity: Activity): Unit

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.

finishAfterTransition

open static fun finishAfterTransition(@NonNull activity: Activity): Unit

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.

getReferrer

@Nullable open static fun getReferrer(@NonNull activity: Activity): Uri?

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

invalidateOptionsMenu

open static fun invalidateOptionsMenu(activity: Activity!): Boolean

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
Return
Boolean: true if this operation was supported and it completed; false if it was not available.

postponeEnterTransition

open static fun postponeEnterTransition(@NonNull activity: Activity): Unit

recreate

open static fun recreate(@NonNull activity: Activity): Unit

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

@Nullable open static fun requestDragAndDropPermissions(activity: Activity!, dragEvent: DragEvent!): DragAndDropPermissionsCompat?

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

Parameters
dragEvent Activity!: Drag event to request permission for
Return
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

open static fun requestPermissions(@NonNull activity: Activity, @NonNull permissions: Array<String!>, requestCode: Int): Unit

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

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

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 and the results of permission requests will be delivered to its 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 OnRequestPermissionsResultCallback#onRequestPermissionsResult(int, String[], int[]).

When checking whether you have a permission you should use .

Calling this API for permissions already granted to your app would show UI to the user to decided whether the app can still hold these permissions. This can be useful if the way your app uses the data guarded by the permissions changes significantly.

You cannot request a permission if your activity sets to true in the manifest because in this case the activity would not receive result callbacks including OnRequestPermissionsResultCallback#onRequestPermissionsResult(int, String[], int[]).

The RuntimePermissions sample app demonstrates how to use this method to request permissions at run time.

Parameters
activity Activity: The target activity.
permissions Activity: The requested permissions. Must me non-null and not empty.
requestCode Activity: Application specific request code to match with a result reported to OnRequestPermissionsResultCallback#onRequestPermissionsResult(int, String[], int[]). Should be >= 0.

requireViewById

@NonNull open static fun <T : View!> requireViewById(@NonNull activity: Activity, @IdRes id: Int): T

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

Note: In most cases -- depending on compiler support -- the resulting view is automatically cast to the target class type. If the target class type is unconstrained, an explicit cast may be necessary.

Parameters
id Activity: the ID to search for
Return
T: a view with given ID

setEnterSharedElementCallback

open static fun setEnterSharedElementCallback(@NonNull activity: Activity, @Nullable callback: SharedElementCallback?): Unit

When android.app.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. This requires android.view.Window#FEATURE_CONTENT_TRANSITIONS.

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

setExitSharedElementCallback

open static fun setExitSharedElementCallback(@NonNull activity: Activity, @Nullable callback: SharedElementCallback?): Unit

When android.app.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. Most calls will only come when returning from the started Activity. This requires android.view.Window#FEATURE_CONTENT_TRANSITIONS.

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

setPermissionCompatDelegate

open static fun setPermissionCompatDelegate(@Nullable delegate: ActivityCompat.PermissionCompatDelegate?): Unit

Sets the permission delegate for ActivityCompat. Replaces the previously set delegate.

Parameters
delegate ActivityCompat.PermissionCompatDelegate?: The delegate to be set. null to clear the set delegate.

shouldShowRequestPermissionRationale

open static fun shouldShowRequestPermissionRationale(@NonNull activity: Activity, @NonNull permission: String): Boolean

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 Activity: A permission your app wants to request.
Return
Boolean: Whether you can show permission rationale UI.

startActivityForResult

open static fun startActivityForResult(@NonNull activity: Activity, @NonNull intent: Intent, requestCode: Int, @Nullable options: Bundle?): Unit

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 Activity: The description of the activity to start.
requestCode Activity: If >= 0, this code will be returned in onActivityResult() when the activity exits.
options Activity: 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.

startIntentSenderForResult

open static fun startIntentSenderForResult(@NonNull activity: Activity, @NonNull intent: IntentSender, requestCode: Int, @Nullable fillInIntent: Intent?, flagsMask: Int, flagsValues: Int, extraFlags: Int, @Nullable options: Bundle?): Unit

Start new IntentSender 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 Activity: The IntentSender to launch.
requestCode Activity: If >= 0, this code will be returned in onActivityResult() when the activity exits.
fillInIntent Activity: If non-null, this will be provided as the intent parameter to IntentSender#sendIntent.
flagsMask Activity: Intent flags in the original IntentSender that you would like to change.
flagsValues Activity: Desired values for any bits set in flagsMask
extraFlags Activity: Always set to 0.
options Activity: 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

open static fun startPostponedEnterTransition(@NonNull activity: Activity): Unit