Activity Results API: A better way to pass data between Activities

Andrew Japar
ProAndroidDev
Published in
2 min readMay 24, 2020

--

As we know passing data and getting responses between two activities are the most common thing we do during the development phase for a long time ago. Currently, the only way to get responses back from the next activity is passing Intent and requestCode through startActivityForResult. After that, we have to check the request and result code before parsing responses on onActivityResult. It might look like this sample code:

Maybe some of you agree that when the app grows, there will be a lot of nested if, the code will be tightly coupled and more difficult to test it.

Let’s Try Activity Results API

I’m still not sure if it’s gonna change again in the future since Activity Results API is still in the Alpha stage but I believe if there is a change, it won’t be a big change. Let’s try to implement it!

First, don’t forget to add this to your Gradle dependency

The version may have been updated please refer to this link https://developer.android.com/jetpack/androidx/releases/activity

Second, create your result contract by extending an abstract class called ActivityResultContract<I,O>. I means type of input and O means type of output. And then you only need to override 2 methods.

And finally, the last step is registering the contract to Activity. You need to pass your custom contract and callback into registerForActivityResult.

Voila! you are done. As you see the code above, there will be no nested if anymore and you can remove all request code constant because it will be maintained by ComponentActivity class.

But wait there’s more! Pre-built contracts for the rescue

Surprisingly, Google provide pre-built contract that for commonly used task such as:

StartActivityForResult()
RequestMultiplePermissions()
RequestPermission()
TakePicturePreview()
TakePicture()
TakeVideo()
PickContact()
CreateDocument()
OpenDocumentTree()
OpenMultipleDocuments()
OpenDocument()
GetMultipleContents()
GetContent()

Actually StartActivityForResult is very similar to custom contract that I explained above. But the difference is you don’t need to specifically define the type of input and output because sometimes you might just need the RESULT_OK to trigger some tasks.

And last but not least, requesting permission will be easier than before. Last time, I created another class just for handling multiple permissions but now they provide RequestPermission and RequestMultiplePermission contract.

Conclusion

The new Activity Result API offers an easier way to do a lot of common tasks especially requestPermission , takePicture and takePhoto so we don’t need to rely on any other third party library anymore. Besides that, it also simplifies the old implementation that might make the code loose coupling, improve the reusability, and easier to test.

If you guys have suggestions or feedback, I’m very happy to hear.

Have a nice day! 😎

--

--

Android @ Cartrack Singapore | Startup Enthusiast | Part Time Traveller | Catch me at https://andrewjap.dev