Android Academy · Lesson

Requesting Permissions in Compose

Use the Accompanist or activity result APIs.

Lesson 2 of 413 steps

Requesting Permissions in Compose is a free Android Academy lesson on CoddyKit. This is lesson 2 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Android Academy learning path, and your progress syncs across the web and the CoddyKit app. The Android Academy course includes 4 lessons in total.

Requesting in Compose

In Jetpack Compose you request runtime permissions using the Activity Result API, wrapped by rememberLauncherForActivityResult. This avoids the old onRequestPermissionsResult callback.

The RequestPermission Contract

The Activity Result API uses contracts that define input and output. ActivityResultContracts.RequestPermission() takes a permission string and returns a Boolean grant result.

Creating a Launcher

rememberLauncherForActivityResult creates a launcher tied to the composition. You pass the contract and a callback that receives the result.

val launcher = rememberLauncherForActivityResult(
    contract = ActivityResultContracts.RequestPermission()
) { isGranted ->
    if (isGranted) {
        // permission granted, use the feature
    } else {
        // permission denied
    }
}

Launching the Request

Call launcher.launch with the permission you want. The system shows its dialog, and your callback fires with the user choice.

Button(onClick = {
    launcher.launch(Manifest.permission.CAMERA)
}) {
    Text("Enable Camera")
}

Check Before You Ask

Do not blindly launch a request. First check whether the permission is already granted, and only request if it is not.

val context = LocalContext.current
val alreadyGranted = ContextCompat.checkSelfPermission(
    context, Manifest.permission.CAMERA
) == PackageManager.PERMISSION_GRANTED

Tracking State in Compose

Hold the current grant status in state so the UI can react. Update it inside the launcher callback.

var hasCamera by remember {
    mutableStateOf(
        ContextCompat.checkSelfPermission(
            context, Manifest.permission.CAMERA
        ) == PackageManager.PERMISSION_GRANTED
    )
}

val launcher = rememberLauncherForActivityResult(
    ActivityResultContracts.RequestPermission()
) { granted -> hasCamera = granted }

Requesting Multiple Permissions

For several permissions at once, use the RequestMultiplePermissions contract. The callback receives a map of permission to grant status.

val multi = rememberLauncherForActivityResult(
    ActivityResultContracts.RequestMultiplePermissions()
) { results ->
    val fineLocation = results[Manifest.permission.ACCESS_FINE_LOCATION] == true
}

multi.launch(
    arrayOf(
        Manifest.permission.ACCESS_FINE_LOCATION,
        Manifest.permission.ACCESS_COARSE_LOCATION
    )
)

Driving the UI From State

Show different content based on the grant state: if granted, show the feature; otherwise show a button to request it. This keeps the screen reactive and clear.

if (hasCamera) {
    CameraPreview()
} else {
    Button(onClick = { launcher.launch(Manifest.permission.CAMERA) }) {
        Text("Grant camera access")
    }
}

The Accompanist Permissions Option

The Accompanist Permissions library offers higher-level helpers like rememberPermissionState. Many teams prefer the raw Activity Result API to avoid an extra dependency; both are valid.

Do Not Request on Launch

Avoid firing a permission request the instant a screen appears. Wait until the user taps an action that clearly needs it. Context-driven requests get far higher grant rates.

Remember Configuration Changes

rememberLauncherForActivityResult survives recomposition, and the Activity Result framework correctly delivers results even across configuration changes like rotation. You do not manage that plumbing yourself.

Quick Check

Test your understanding of requesting permissions.

Recap

You requested permissions the modern way:

  • Use rememberLauncherForActivityResult with the RequestPermission contract
  • Check first, then launcher.launch(permission)
  • Track grant status in state to drive the UI
  • Use RequestMultiplePermissions for several at once

Next: handling denials and showing rationale.

Free to start

Learn Kotlin with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
36
Lessons
152

Frequently Asked Questions

Is the “Requesting Permissions in Compose” lesson free?

Yes — any 3 lessons of the Android Academy learning path, including “Requesting Permissions in Compose”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Android Academy course includes 4 lessons in total.

What will I learn in “Requesting Permissions in Compose”?

Use the Accompanist or activity result APIs. You practise Android Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Android Academy?

No prior experience is required. Android Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.

How long does the “Requesting Permissions in Compose” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Android Academy lesson?

Yes. Every Android Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. The Android Permission Model
  2. Requesting Permissions in Compose
  3. Handling Denials and Rationale
  4. Best Practices for Privacy
← Back to Android Academy