Asking for location permission, grantResults is empty

Ask A Question

Login To post a question

I’m having some trouble asking for the location permission. The activity extends AppCompatActivity

This is the request

if (ActivityCompat.checkSelfPermission(this,
    android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

    arrayOf(android.Manifest.permission.ACCESS_FINE_LOCATION), LOCATION_PERMISSION_RC)


And this is how I check the response

override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
    if (requestCode == LOCATION_PERMISSION_RC) {
        if (grantResults.isNotEmpty()) {
            if ( grantResults[0] == PackageManager.PERMISSION_GRANTED ) {
                // permission was granted
                Toast.makeText(this, "Permission granted", Toast.LENGTH_LONG).show()
            } else {
                Toast.makeText(this, "Permission denied", Toast.LENGTH_LONG).show()
        } else {
            Toast.makeText(this, "grantResults is empty", Toast.LENGTH_LONG).show()

What happens is that if I grant the permission, the app shows the toast message where it says that grantArray is empty, but the permission has been actually granted, I can use MyLocation layer.

According to the documentation:

grantResults - int: The grant results for the corresponding permissions which is either PERMISSION_GRANTED or PERMISSION_DENIED. Never null.

while I get an empty array.

This is not really a problem since it would be enough to remove the toast, but I want to understand why that happens and how to fix this behaviour.

For more info please click here

Have a question or need a custom quote?