The “simplicity” of managing permissions in Android 11

Carlos Daniel
3 min readAug 6, 2020

Spanish version available.

Thanks to request permission features that come with Android 11 news, there is a new way to ask for permissions within an app. These are part of the new Register Activity for Result API that comes to replace the startActivityForResult.

The most interesting thing, so far, is that it turns the way of requesting permissions into a component that can be more maintainable, scalable and easy to implement. (Note: As these API dependencies are currently in alpha, it is VERY likely that they will change something over time, but the general structure I think will be preserved, and see how easy it is now to request some permission).

Adding the dependencies:

We start adding the required dependencies for it, activity and fragment ktx in our app’s build.gradle file:

In the example app, we are going to ask the permissions just in the moment we click on the camera icon (as is the current suggestion, only requesting the permissions just when actually are needed), therefore we must add the use of it to the AndroidManifest.xml.

<uses-permission android:name="android.permission.CAMERA" />

Now, using Register Activity for Result API (registerForActivityResult()), we can have an object that let us launch it every time we ask for the permission and we pass as parameter the proper contract to ask it (ActivityResultContracts.RequestPermission()) and the callback of the next parameter remains as a boolean lambda that indicates whether or not it was granted.

Right here then we would put the logic of what to do when the permission is or is not granted, including the scenario when the user denies it the first time (shouldShowRequestPermissionRationale) with the idea of properly explaining what happens if it is not granted (for versions prior to Android 11 it would show the Don’t ask again box):

Finally, to launch the permission under a certain action (as suggested), then when we click the camera button, we call the newly created permission object, and with the *launch* method we pass the permission that we want to request and it’s done:

Showing rationale and granted permission Android 11

… and that’s all! we don’t need anything else. Thus, we could replace or include permission groups such as the Location or Read / Write of the Storage. Remember that for Android 11 it will show you the One-Time Request options or enable only when the app is used, for previous versions, the classic options will always be available:

Showing rationale and granted permission Android below 11

A new scenario can happen: when the permission is definitively denied, it’s possible that it can really be required it. Then it is suggested that to take the user to the device Settings screen so that from there they can reconfigure the permissions directly. To do this, just create an Intent with the required screen (Default settings) and start a new activity:

Calling settings intent

This is all!! what do you think about it? simple, right? in this way we write less code, we make use of the available KTX and we do the proper management of the permissions.

The example project is found on Github:

--

--

Carlos Daniel

Android & Flutter Developer. GDE for Android & Mobile Engineer.