ChandraSaiMohan bhupathi
7 min readAug 9, 2021

--

Android Runtime permissions

Android Runtime permissions with AndroidX RequestPermission contract — simple sample

1.Before you begin:

2.Getting Setup :

gitclone: https://github.com/chandragithub2014/PermissionsExample.git

Workflow for requesting permissions:

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.

--

--