ChandraSaiMohan bhupathi
7 min readAug 9, 2021


Android Runtime permissions

Android Runtime permissions with AndroidX RequestPermission contract — simple sample

1.Before you begin:

In this blog we will learn how to implement Android runtime permissions and how to declare them if app is installed on devices that runs Android 6.0 (API level 23) or higher .

use the RequestPermission contract, included in an AndroidX library, where we allow the system to manage the permission request code rather than handling with callbacks.


App permissions help support user privacy by protecting access to the following:

  • Restricted data, such as system state and a user’s contact information.
  • Restricted actions, such as connecting to a paired device and recording audio.

Types of permissions:

  1. Install-time permissions
  2. Normal permissions
  3. Signature permissions
  4. Runtime permissions

In this blog we will be discussing on runtime permissions

What are Runtime permissions?

Runtime permissions, also known as dangerous permissions, give your app additional access to restricted data, and they allow your app to perform restricted actions that more substantially affect the system and other apps. Therefore, you need to request runtime permissions in your app before you can access the restricted data or perform restricted actions. When your app requests a runtime permission, the system presents a runtime permission prompt

Many runtime permissions access private user data, a special type of restricted data that includes potentially sensitive information. Examples of private user data include location and contact information.


  • Experience with Kotlin syntax, including lambdas.
  • A basic understanding of Navigation component, permissions, Android Fragments, Activities

What you’ll do ?

A sample application with below two screens that demonstrates single/multiple runtime permissions:

  1. Launch Camera screen : This Fragment demonstrates providing camera permission at runtime and allows to launch camera when granted permissions. This screen allows to navigate to next screen.
  2. Contacts Screen: This Fragment demonstrates accessing multiple permissions at runtime and allows to read/write contacts from device when granted permissions.

2.Getting Setup :

The code is available in the above Github repository.


3. Run the App:

Screen shots for different scenarios:



This class contains various methods to handle Runtime permissions. This class is single point of contact for all the runtime permission handling. Activity/Fragment can use this helper class to handle all the runtime permissions.

Workflow for requesting permissions:

  1. Determine whether app was already granted permission:

ContextCompat.checkSelfPermission(): Checks if the user has already granted your app a particular permission.

This method returns either PERMISSION_GRANTED or PERMISSION_DENIED, depending on whether your app has the permission.

This is the first method that needs to be called as part of checking whether particular permission is granted or not.

Code sample for the above step:

Where context is fragment’s context and manifestPermission is the permission to be checked whether it is granted or not and it should be declared in manifest file.

In this example “manifestPermission” is Manifest.permission.CAMERA.

2.Explain why app needs permission :

In the above step if the “ContextCompat.checkSelfPermission()” method returns PERMISSION_DENIED, then call shouldShowRequestPermissionRationale().If this method returns true, show an educational UI to the user. In this UI, describe why the feature, which the user wants to enable, needs a particular permission.

In this app , I will be displaying an alert dialog that shows purpose of the permission. If users clicks “OK” button in alert dialog, I will be shown system permission dialog to either grant/deny permission.

3.Request permissions:

After the user views an educational UI (Alert Dialog in our case) , if the users clicks “OK” button in Alert Dialog to again request the permission then Users see a system permission dialog, where they can choose whether to grant a particular permission to your app.

Important Note:

Traditionally, we can manage a request code ourselves as part of the permission request and include this request code in your permission callback logic.

Another option is to use the RequestPermission contract, included in an AndroidX library, where you allow the system to manage the permission request code for you. Because using the RequestPermission contract simplifies your logic, it’s recommended that you use it when possible.

RequestPermission contract :

RequestPermission contract contains 2 classes:

RequestPermission:To request a single permission

2. RequestMultiplePermissions:To request multiple permissions at the same time.

In your activity or fragment’s initialization logic, pass in an implementation of ActivityResultCallback into a call to registerForActivityResult(). The ActivityResultCallback defines how your app handles the user's response to the permission request.

Keep a reference to the return value of registerForActivityResult(), which is of type ActivityResultLauncher.

To display the system permissions dialog when necessary, call the launch() method on the instance of ActivityResultLauncher that you saved in the previous step.

Code sample for the above explained steps:



Using launch():

Where manifestPermissions: Array of Multiple permissions .(In our case it is READ and WRITE contacts)

where manifestPermission: Is the single permission (In our case it is CAMERA)

PermissionHelper: Putting all discussed above in a single class

In the above we have two functions to handle single/multiple permissions as part of explanation purpose:

PermissionListener: This is the interface implemented in Fragment/Activity to display alert dialog(Display info about importance of permission) or open camera or allow read/write contacts when permission is granted.


This the fragment that allows to access camera if permission is Granted. Also contains a button to navigate to next screen.

This is the place where we initialize PermissionHelper class and handle callbacks to display AlertDialog and launch camera.

Initialize Permission Helper:


This Fragment demonstrates accessing multiple permissions at runtime and allows to read/write contacts from device when granted permissions.

In the above code sample we initialize PermissionHelper and make a call to checkForMultiplePermissions() method which array of permissions declared in manifest.


In this blogs, we have covered handling Runtime permissions in Android app using RequestPermission contract, included in an AndroidX library instead of handling ourselves to simplify permission handling with a simple example.

PermissionsHelper can be included in any project to handle runtime permissions and customize accordingly as per needs in callbacks.