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?
It exists because there are two types of
Acvivity
in Android,android.app.Activity
docs, andandroid.support.v4.app.ActivityCompat
docs.Activity
is for use in devices whose min SDK version is 14 iircActivityCompat
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.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.
That wholly depends on which type of
Activity
you are using. If your activity is a child ofActivity
then useActivity.requestPermissions(permissions, requestCode)
. if you are using a child ofActivityCompat
however, useActivityCompat.requestPermissions(activity, permissions, requestCode)
.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).Activity#requestPermissions()
is for apps with aminSdkVersion
of 23 or higher, or for apps whose developers like callingif (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).If your
minSdkVersion
is 23 or higher, feel free to useActivity#requestPermissions()
. Otherwise, I recommendActivityCompat.requestPermissions()
.