5

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?

Roshana Pitigala
  • 8,437
  • 8
  • 49
  • 80

2 Answers2

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().

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Will the runtime permission dialog popup, if I use `ActivityCompat.requestPermissions()` and the device is 6.0 or higher? – Roshana Pitigala Jun 25 '19 at 20:04
  • @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. – CommonsWare Jun 25 '19 at 20:08
  • So I assume, `ActivityCompat.requestPermissions()` is just a lazy way; to get rid of doing another check `if(Build.VERSION.SDK_INT >= 23)` – Roshana Pitigala Jun 25 '19 at 20:09
  • 1
    @RoshanaPitigala: It is a lazy way to skip writing another `SDK_INT` check, plus remembering which version number to check against. – CommonsWare Jun 25 '19 at 20:13
  • 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](https://stackoverflow.com/a/56761651/4605699) has more on that. – ItIsEntropy Jun 25 '19 at 20:42
  • 1
    @Pure-Entropy: `ActivityCompat` does not require an `AppCompatActivity`. – CommonsWare Jun 25 '19 at 20:43
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).

ItIsEntropy
  • 306
  • 3
  • 13
  • `ActivityCompat` is not an `Activity`. See: https://developer.android.com/reference/androidx/core/app/ActivityCompat. – CommonsWare Jun 25 '19 at 20:46