RIP onActivityResult(), Welcome ActivityResult API

Rajat Dhamija
AppyHigh Blog
Published in
2 min readSep 13, 2020

Passing data between activities has always been a common thing in Android Development. The traditional way was using startActivityForResult() and onActivityResult api to start an Activity and receive back some data.

Now as AndroidJetpack being widely used it is recommended to use Activity Result APIs introduced in AndroidX Activity 1.2.0-alpha08 and Fragment 1.3.0-alpha08.

Most of you will see as the app code base grows, there will be a lot of code added and it might be difficult maintain it after some time. You now have a nice abstraction which allows you to handle onActivityResult() method in a very clean and reusable way!

3 Steps to implement ActivityResult API

Step 1: First thing is adding the dependencies to you build.gradle file

Step 2: Now create your result contract by extending an abstract class called ActivityResultContract<Input,Output>. Input means type of input and Output means type of output. And then you only need to override methods :
createIntent and parseResult

Step 3: Registering the contract in the activity

All done! As you see now there is no nesting of code and you have eliminated all the request code Constants as well as they are maintained by the ComponentActivity class.

Some Ready to Use Contracts —

You can find more here

Requesting Permissions

Requesting permissions has always been quite a task for developers, but not anymore 😍, we have RequestPermission and RequestMultiplePermission contract for handling that with ease.

Here is a sample code for you to play around! Cheers 😎

Liked my work? Buy me a coffee.

--

--