ActivityResult API Android…

Today we will learn about ActivityResult API, which makes the life of Android developers a lot easier to manage the permissions, and inter-activity communication.
- Currently if we need some data from child activity or another activity started by host activity we use startActivityForResult and onActivityResult
// start activity
startActivityForResult(intent,requestCode)// get the result
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
}
- Second use case is permissions, if you ever implemented the asking permissions ex. access media files, etc. You got it.
// request for permisison
requestPermissions(arrayOf(android.Manifest.permission.READ_CALENDAR))// manage the result
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray
) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
}
Now we will see how the above mentioned use-case’s👆 implementation got more easier with ActivityResult API
First things First :
- Dependencies : Add the 👇 following 2 dependencies in your app level build.gradle file
// check the latest version @Android released notes
implementation "androidx.activity:activity-ktx:1.2.0-beta02"
implementation 'androidx.fragment:fragment-ktx:1.3.0-beta02'
Request permissions : There are 2 ways to request permissions provided by this API.
ActivityResultContracts.RequestPermission()) : If you want to request single permission this contract will do the job for you.
- Create ActivityRequestLauncher with RequestPermisison contract
Lambda in the following code 👇 will execute when the request finishes, you can call it onRequestPermissionsResult.
It returns Boolean in this case based on if permission is granted or not

- Now that’s it you are good to go to request any permisison and it will do the job for you

- There are many other prebuilt contract provided by API that you can use directly like RequestPermisison. Check the Reference section to learn more about.
If you want to request multiple permissions use the following contract:

Here result 👆 is of type Map<String,Boolean>, which have permission name as a key and value based on if permision is granted or not
Now we will see, How we can use this API to get data from the child activity.
- We will create custom contract which extend the ActivityResultContract<Input, Output>()
Input : If you need to send some data
Output : Result that you expect from the activity that you started for result
- Here 👇 we have created custom contract which take Int as input and gives us result of type String

- There are two methods that we need to override :
// here we handle the resultparseResult(resultCode: Int, intent: Intent?): String? // it's the Output type that we have defined for our contract
// this method creates the Intent that we defined, input:Int? It's the input type that we have defined for our contractcreateIntent(context: Context, input: Int?): Intent
- Let’s use this contract :

- Here is the Github repo for this article :