By Yacine Rezgui

Whether you’re requesting permissions in an application, selecting a file from a file management system, or expecting data from a third-party application, passing data between activities is the core of interprocess communication in Android. We recently released a new ActivityResult API to help simplify data communication between activities.

Previously, to get a return result from an Activity that was started, the application needed to implement the onActivityResult() method on both the Activity and Fragment, and then check which requestCode the callback was associated with. Verify that the result of the requestCode is OK, and finally verify the returned or extended data.

However, this approach makes our code very complex and does not guarantee type-safe parameters when the Activity sends or receives data.

What is the ActivityResult API

The ActivityResult API is added to Jetpack’s Activity and Fragment libraries to simplify the processing of data from activities by providing type-safe contracts. These protocols define the expected input and output types for common operations, such as taking a photo or requesting permissions, but you can also customize the protocols to suit different scenarios.

The ActivityResult API provides components for registering the Activity’s processing results, making requests, and processing the results immediately after the system returns them. You can also use a separate class where the Activity is started to receive the returned results, which is still type-safe.

How to use

Let’s use the ActivityResult API with an example of opening a document.

First, you need to add the following dependencies to your Gradle file:

Repositories {Google () Maven ()} dependencies {// in https://developer.android.google.cn/jetpack/androidx/releases/activity for the latest version number def activity_version = "1.2.0" / / in https://developer.android.google.cn/jetpack/androidx/releases/fragment for the latest version number def fragment_version = "1.3.0" implementation "androidx.activity:activity:$activity_version" implementation "Androidx. Fragments, fragments: $fragment_version}"Copy the code

You need to register a callback in the protocol that defines the types of its inputs and outputs.

In the following code, GetContent() refers to the ACTION_GET_DOCUMENT Intent, which is one of the default protocols defined in the Activity library. You can find a complete list of defined protocols here.

val getContent = registerForActivityResult(GetContent()) { uri: Uri? ->
    // Process the returned Uri
}
Copy the code

Now we need to start our Activity with the returned launcher. You can set a mime type filter to filter the selected file, and getContent.launch () takes a string as an argument:

 val getContent = registerForActivityResult(GetContent()) { uri: Uri? ->
    // Process the returned Uri
}
 
override fun onCreate(savedInstanceState: Bundle?). {
    // ...
 
    val selectButton = findViewById<Button>(R.id.select_button)
 
    selectButton.setOnClickListener {
        // Pass in the MIME type you want the user to select as input
        getContent.launch("image/*")}}Copy the code

Once the image is selected and returned to your Activity, the callback function you registered is executed with the expected result. As you can see in the code snippet, ActivityResult makes for a much easier development experience when handling data returned from activities.

Now use the latest stable versions of the Activity and Fragment libraries to handle your Intent results in a type-safe way through the ActivityResult API.

  • View the latest version of the Activity library
  • View the latest version of the Fragment library

We’d also like to hear feedback from developers, if you have any suggestions or comments, please feel free to submit them to us here.