Michael
Michael

Reputation: 143

Request multiple permissions by requestPermissions() but onRequestPermissionsResult receives only single permission result

I'm implementing app permission flow to use geo location. I requested ACCESS_FINE_LOCATION and ACCESS_COARSE_LOCATION by requestPermissions() in a Fragment.

requestPermissions(
  arrayOf(Manifest.permission.ACCESS_COARSE_LOCATION,
    Manifest.permission.ACCESS_FINE_LOCATION),
    PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION
  )

companion object {
        private const val PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION = 1
  :
}

And I received the request permissions result by onRequestPermissionsResult() in Fragment.

override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
        when (requestCode) {
            PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION -> {
                if (grantResults.isNotEmpty() &&
                    grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    // User granted permission in system dialog
                } else {
                  // User denied permissions
                }
            }
        }
    } 

But onRequestPermissionsResult() strangely received only single permission which was ACCESS_COARSE_LOCATION in the second parameter as permissions: Array. And checkSelfPermission() also returned false even choosing allow in a system dialog.

// this condition returns false after receiving permission result on onRequestPermissionsResult()
ContextCompat.checkSelfPermission(requireContext(),
                Manifest.permission.ACCESS_FINE_LOCATION)
                == PackageManager.PERMISSION_GRANTED &&
                ContextCompat.checkSelfPermission(requireContext(),
                    Manifest.permission.ACCESS_COARSE_LOCATION) {
}

I wonder why onRequestPermissionsResult() received only ACCESS_COARSE_LOCATION as the result, even though I request multiple permissions by giving array of ACCESS_COARSE_LOCATION, ACCESS_FINE_LOCATION when I call requestPermissions().

Could you please explain to me why permissions request only returns the single permission result?

Thank you in advance for your help.

Upvotes: 0

Views: 1378

Answers (1)

Charitha Ratnayake
Charitha Ratnayake

Reputation: 379

You need to use only single permission for ACCESS_FINE_LOCATION. You don't need to use both. Because your request permission ACCESS_FINE_LOCATION is include both ACCESS_FINE_LOCATION and ACCESS_COARSE_LOCATION.

https://developer.android.com/training/location/retrieve-current#Permission

For handle multiple permissions I'm highly recommended the below library is perfectly handle every scenarios.

Dexter: com.karumi:dexter:6.#.#

For multiple permissions you could use:

Dexter.withContext(this)
    .withPermissions(
        Manifest.permission.CAMERA,
        Manifest.permission.READ_CONTACTS,
        Manifest.permission.RECORD_AUDIO
    ).withListener(new MultiplePermissionsListener() {
        @Override public void onPermissionsChecked(MultiplePermissionsReport report) {/* ... */}
        @Override public void onPermissionRationaleShouldBeShown(List<PermissionRequest> permissions, PermissionToken token) {/* ... */}
    }).check();

Good luck

Upvotes: 0

Related Questions