OnRequestPermissionsResult deprecated Android Java

OnRequestPermissionsResult deprecated Android Java

1. Overview

In this article, we will discuss the new Activity Result API, an alternative to the deprecated onRequestPermissionsResult + requestPermissions methods in Android Java.

Android introduced this powerful Activity Result API as a major change in the androidx.activity:activity:1.2.0 and androidx.activity:activity-ktx:1.2.0 releases.

This API helps us to deal with permission requests in a simpler way. Whenever an app requires more than one permission from the user, it brought in more complexity and had to write a lot more messy code with the old request permissions approach. However, the new API handles all the complexities so making it painless.

2. Old way using onRequestPermissionsResult and requestPermissions

In the deprecated approach, we perform the following to request permissions from the user:

1. Call requestPermissions method and pass the permission required from the user along with the request code.

requestPermissions(CONTEXT,
                arrayOf(Manifest.permission.REQUESTED_PERMISSION),
                REQUEST_CODE)

After the user performs an action, then the onRequestPermissionsResult call back is invoked with the request code which is passed in the requestPermissions method. The request code helps to uniquely identify the permission.

override fun onRequestPermissionsResult(requestCode: Int,
        permissions: Array<String>, grantResults: IntArray) {
    when (requestCode) {
        PERMISSION_REQUEST_CODE -> {
            // If request is cancelled, the result arrays are empty.
            if ((grantResults.isNotEmpty() &&
                    grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
                // Permission is granted
            } else {
                // Permission is not granted
            }
            return
        }
        else -> {
            // Ignore all other requests.
        }
    }
}

3. Alternative to deprecated onRequestPermissionsResult

The Activity Result APIs provide the registerForActivityResult in place of onRequestPermissionsResult + requestPermissions.

The registerForActivityResult method takes the following two parameters and returns ActivityResultLauncher as output:

1. ActivityResultContracts

A contract defines the type of action or interaction (Intent) requested from an activity. After getting the result from the activity, it then produces the output of a particular type that makes calling activity for result type-safe.

A collection of standard ActivityResultContracts available for use. Among the contracts available, we had to use the RequestPermission contract for requesting single permission at a time or the RequestMultiplePermissions contract for requesting multiple permission at a time.

2. ActivityResultCallback

The callback is to be called once the activity result is available. Whatever code you plan to write in your old onRequestPermissionsResult method must be placed here.

3.1. Requesting single permission

For example, the below registerForActivityResult method takes the ActivityResultContracts.RequestPermission as contract and then returns the ActivityResultLauncher as output.

var requestSinglePermission = registerForActivityResult(
    ActivityResultContracts.RequestPermission()) { isGranted ->
    if (isGranted) {
       // Permission is granted. Continue the action or workflow
                // in your app.
    } 
}

Once you have the ActivityResultLauncher, you can request permission using the launch method:

requestSinglePermission.launch(Manifest.permission.WRITE_CONTACTS)

3.2. Requesting multiple permissions

For example, the below registerForActivityResult method takes the ActivityResultContracts.RequestMultiplePermissions as contract and then returns the ActivityResultLauncher as output.

var requestSinglePermission = registerForActivityResult(
    ActivityResultContracts.RequestMultiplePermissions()) { permissions ->
            permissions.entries.forEach {
                // check whether each permission is granted or not
    }
}

Once you have the ActivityResultLauncher, you can request permission using the launch method:

requestSinglePermission.launch(
                arrayOf(
                    Manifest.permission.READ_CONTACTS,
                    Manifest.permission.ACCESS_FINE_LOCATION
                ))

Notice that we haven’t passed any request code here as registerForActivityResult automatically takes care of everything.

4. Conclusion

To sum up, we have discussed the Activity Result API available from androidx 1.2.0 releases as an alternative to the deprecated onRequestPermissionsResult and requestPermissions methods in Android Java. You can also refer settings panel article where we are using the Activity Result API for launching the settings panel.

Leave a Reply

Your email address will not be published. Required fields are marked *