Android Permission Manager — Complete Guide
In this tutorial, you'll learn about Android Permission Manager. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.
The Problem
Permission request logic is scattered across every Activity and Fragment. You repeat the same rationale dialog, settings redirect, and callback handling in multiple places.
Wrong Approach ❌
// Duplicate permission logic in every screen
class Screen1Activity : AppCompatActivity() {
fun requestCamera() {
requestPermissions(arrayOf(CAMERA), 100)
}
}
class Screen2Activity : AppCompatActivity() {
fun requestCamera() {
requestPermissions(arrayOf(CAMERA), 100) // Same logic duplicated!
}
}
Output: Inconsistent permission handling, code duplication, hard to maintain.
Right Approach ✅
// Centralized permission manager
class PermissionManager(private val context: Context) {
sealed class PermissionResult {
object Granted : PermissionResult()
object Denied : PermissionResult()
object PermanentlyDenied : PermissionResult()
}
suspend fun checkAndRequestPermission(
activity: Activity,
permission: String,
rationaleMessage: String
): PermissionResult {
return when {
hasPermission(permission) -> PermissionResult.Granted
shouldShowRationale(activity, permission) -> {
showRationaleDialog(activity, rationaleMessage)
val result = requestPermission(activity, permission)
handleResult(activity, permission, result)
}
else -> {
val result = requestPermission(activity, permission)
handleResult(activity, permission, result)
}
}
}
private fun hasPermission(permission: String): Boolean {
return ContextCompat.checkSelfPermission(context, permission) == PERMISSION_GRANTED
}
private fun handleResult(activity: Activity, permission: String, granted: Boolean): PermissionResult {
return if (granted) {
PermissionResult.Granted
} else if (!shouldShowRationale(activity, permission)) {
PermissionResult.PermanentlyDenied
} else {
PermissionResult.Denied
}
}
fun openAppSettings(activity: Activity) {
Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS).apply {
data = Uri.fromParts("package", context.packageName, null)
activity.startActivity(this)
}
}
}
// Usage in Activity
class ProfileActivity : AppCompatActivity() {
private val permissionManager = PermissionManager(this)
fun onCameraClick() {
lifecycleScope.launch {
when (permissionManager.checkAndRequestPermission(this@ProfileActivity, CAMERA, "Need camera for photos")) {
PermissionResult.Granted -> openCamera()
PermissionResult.PermanentlyDenied -> permissionManager.openAppSettings(this@ProfileActivity)
PermissionResult.Denied -> showFeatureInfo()
}
}
}
}
Output: Centralized, consistent permission management.
Prevention
- Create a
PermissionManagerclass to encapsulate permission logic. - Return a sealed result type (
Granted,Denied,PermanentlyDenied). - Use coroutines for async permission flow.
- Centralize the settings redirect logic in one place.
Common Mistakes with permission manager
- Misunderstanding that
Stringis[Char]with poor performance for large text operations - Using
foldlinstead offoldl'causing stack overflow on large lists - Forgetting
deriving (Show, Eq)on custom data types needed for debugging
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