Android Permission Request — Complete Guide
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.RequestPermissionwith the new API.
Common Mistakes with permission request
- Using
returnto exit a function early instead of wrapping a pure value in the monad - Mixing let bindings with <- bindings in do notation, producing type errors
- 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
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro