Activity.requestPermissions vs ActivityCompat.requestPermissions
Asked Answered
P

2

6

Runtime permission dialog is shown in Android 6.0 or higher, so Activity.requestPermissions(...) which was added in API level 23 makes sense.

But why is there another one (ActivityCompat.requestPermissions(...)) to be used for below Android 6.0? Does this show runtime permission request dialog in lower versions?


  • What is the difference between

    Activity.requestPermissions(permissions, requestCode)
    

    and

    ActivityCompat.requestPermissions(activity, permissions, requestCode)
    
  • Which one should I use?

Phototopography answered 25/6, 2019 at 19:47 Comment(0)
U
6

Does this show runtime permission request dialog in lower versions?

No. There is no such dialog on lower versions. It will simply call your onRequestPermissionsResult() method to let you know that you hold the permissions (since, by definition, you already do).

What is the difference between

Activity#requestPermissions() is for apps with a minSdkVersion of 23 or higher, or for apps whose developers like calling if (Build.VERSION.SDK_INT >= 23) to avoid that call on older devices.

ActivityCompat.requestPermissions() is for any app, as it "does the right thing" on all supported API levels (back to API Level 14 IIRC).

Which one should I use?

If your minSdkVersion is 23 or higher, feel free to use Activity#requestPermissions(). Otherwise, I recommend ActivityCompat.requestPermissions().

Underbody answered 25/6, 2019 at 20:2 Comment(6)
Will the runtime permission dialog popup, if I use ActivityCompat.requestPermissions() and the device is 6.0 or higher?Phototopography
@RoshanaPitigala: Yes, assuming that the user has not clicked "don't ask again" for that permission. All ActivityCompat.requestPermissions() does is check Build.VERSION.SDK_INT and call through to Activity#requestPermissions() if the device is running Android 6.0+, with fallback behavior if it is not.Underbody
So I assume, ActivityCompat.requestPermissions() is just a lazy way; to get rid of doing another check if(Build.VERSION.SDK_INT >= 23)Phototopography
@RoshanaPitigala: It is a lazy way to skip writing another SDK_INT check, plus remembering which version number to check against.Underbody
Actually it is not. ActivityCompat.requestPermissions() is for use in activities that are of the ActivityCompat type, while Activity.requestPermissions(permissions, requestCode) is for use in Activities that are of the Activity type. my answer has more on that.Kampmeier
@Pure-Entropy: ActivityCompat does not require an AppCompatActivity.Underbody
K
1

But why is there another one (ActivityCompat.requestPermissions(...)) to be used for below Android 6.0?

It exists because there are two types of Acvivity in Android, android.app.Activity docs, and android.support.v4.app.ActivityCompatdocs.

  • Activity is for use in devices whose min SDK version is 14 iirc
  • ActivityCompat is for backwards compatibility (SDK 9 and above). It allows you to have access to supportable new features and Material themes without any of the breaking changes that the new OS versions introduced to achieve the new features and UI.

Does this show runtime permission request dialog in lower versions?

No. Android 6.0 is the first to show runtime Permission dialogs and as such previous versions of Android cannot show them. That bit of code is actually ignored by previous versions of the OS iirc.

Which one should I use?

That wholly depends on which type of Activity you are using. If your activity is a child of Activity then use Activity.requestPermissions(permissions, requestCode). if you are using a child of ActivityCompat however, use ActivityCompat.requestPermissions(activity, permissions, requestCode).

Kampmeier answered 25/6, 2019 at 20:34 Comment(1)
ActivityCompat is not an Activity. See: developer.android.com/reference/androidx/core/app/….Underbody

© 2022 - 2024 — McMap. All rights reserved.