Skip to content

Android Permission Manager — Complete Guide

DodaTech Updated 2026-06-24 2 min read

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 PermissionManager class 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

  1. Misunderstanding that String is [Char] with poor performance for large text operations
  2. Using foldl instead of foldl' causing stack overflow on large lists
  3. 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

### Should PermissionManager be a Singleton?

Prefer not — it should be scoped to the Activity lifecycle. Use a Factory that takes Context to avoid memory leaks.

### How do I handle multiple permissions in the manager?

Accept List<String> in the request method. Use RequestMultiplePermissions contract internally and return a Map<String, PermissionResult>.

### Can I use PermissionManager in ViewModels?

ViewModels shouldn't hold Context. Pass the permission result state from the UI layer to the ViewModel as a sealed class.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro