Skip to content

Android Permission Request — Complete Guide

DodaTech Updated 2026-06-24 2 min read

In this tutorial, you'll learn about Android Permission Request. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.

The Problem

Your app crashes when a permission is denied, or you request the same permission repeatedly without explaining why, and the user permanently denies it.

Wrong Approach ❌

// Requesting permission without explanation
val permission = Manifest.permission.CAMERA
if (checkSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) {
    requestPermissions(arrayOf(permission), CAMERA_REQUEST_CODE)
}

// No handling of denial
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
    // Empty — user denied, nothing happens
}

Output: User denies once, app never asks again. Camera feature broken silently.

Right Approach ✅

class MainActivity : AppCompatActivity() {
    companion object {
        private const val CAMERA_PERMISSION_REQUEST = 100
    }

    private fun requestCameraPermission() {
        when {
            checkSelfPermission(Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED -> {
                openCamera()
            }
            shouldShowRequestPermissionRationale(Manifest.permission.CAMERA) -> {
                // User previously denied — show rationale
                AlertDialog.Builder(this)
                    .setTitle("Camera Permission Needed")
                    .setMessage("We need camera access to take profile photos.")
                    .setPositiveButton("Grant") { _, _ ->
                        requestPermissions(arrayOf(Manifest.permission.CAMERA), CAMERA_PERMISSION_REQUEST)
                    }
                    .setNegativeButton("Deny") { _, _ ->
                        // Disable camera feature
                        showFeatureDisabledMessage()
                    }
                    .show()
            }
            else -> {
                // First time request
                requestPermissions(arrayOf(Manifest.permission.CAMERA), CAMERA_PERMISSION_REQUEST)
            }
        }
    }

    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
        when (requestCode) {
            CAMERA_PERMISSION_REQUEST -> {
                if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    openCamera()
                } else if (!shouldShowRequestPermissionRationale(Manifest.permission.CAMERA)) {
                    // User checked "Never ask again"
                    showSettingsRedirectDialog()
                }
            }
        }
    }
}

Output: Proper permission flow with rationale, denial handling, and settings redirect.

Prevention

  • Use shouldShowRequestPermissionRationale() before requesting again.
  • Show a rationale dialog when the user previously denied.
  • Detect "Never ask again" and redirect to app settings.
  • Use ActivityResultContracts.RequestPermission with the new API.

Common Mistakes with permission request

  1. Using return to exit a function early instead of wrapping a pure value in the monad
  2. Mixing let bindings with <- bindings in do notation, producing type errors
  3. Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors

These mistakes appear frequently in real-world Android code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### What is shouldShowRequestPermissionRationale()?

It returns true if the user has denied the permission before (but didn't check "Never ask again"). Show a rationale explaining why you need it.

### How do I redirect to app settings?

Create an Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS) with Uri.fromParts("package", packageName, null). Start it with startActivity().

### What happens when the user permanently denies ("Never ask again")?

requestPermissions() will immediately call onRequestPermissionsResult() with DENIED. Check shouldShowRequestPermissionRationale() — it returns false, meaning you must redirect to settings.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro