activityResult; The new way

Sayed El-Abady
MindOrks
Published in
3 min readMar 20, 2020
Photo by Caspar Camille Rubin on Unsplash

Launching the camera or dialing a number or even asking for runtime permission was some sort of a bad experience (for me at least) as you should fire your request and then wait for the result in another method, onActivityResult callback in your activity or fragment and if you happen to have a case when you want to do this in another class you will have to do a workaround or a delegation for this.

The real problem with me in that happens when I have many things I should take care of in the same activity as asking for permission and based on the result do another thing and after that another thing and so on. Can you imagine how messy my onActivityResult method becomes? In the latest release of AndroidX’s Fragment and Activity, they have released a good replacement for that pattern.

The new way of getting a result from an Activity.

This basic concept of this pattern has four three components but before we start don’t forget to add these dependencies to your app Gradle file:

implementation 'androidx.activity:activity:1.2.0-alpha02'
implementation 'androidx.fragment:fragment:1.3.0-alpha02'

ActivityResultLauncher: An interface represents the launcher of the required intent which has a method launch takes input for that intent:

ActivityResultContract: A contract specifying that an activity can be called with an input of type I and produce an output of type O, it is an abstract class that contains 2 methods the first one createIntent in which you create the target intent to the second activity which you want to get results from, and the second one parseResult from which you get your results, here’s the implementation:

ActivityResultCallback: An interface that provides you with the result once it becomes available throw onActivityResult method:

How to?

After we saw the components and what each one of them means let’s see how to connect them together, let’s say that we want to request location permission from the user, and based on the response from the user we show a toast.

  • At first, we want to make our launcher ready, fortunately, there’s a method called prepareCall which is built for that purpose it takes an ActivityResultContract and an ActivityResultCallback
    and returns an ActivityResultLauncher which you’ll use to launch the other activity:
  • Then we want to build our contract and also for that there are some built-in contracts for the most common use-cases like dialing a number, requesting permission, or even opening your own activity result, you can find all of them in a class called ActivityResultContracts.java, here’s what we want from it:
  • The remaining thing is the callback and that’s what we want to write for handling what we want after the user takes the action and that’s the second parameter for our prepareCall now let’s see how we connect all of these together:

That’s the basic idea of how to implement it, in case you wanted to add another intent you will have another handler(ActivityResultLauncher) for it like the permissionRequester.

Please follow this link to know how more and know how to create your own contract.

Stay at home everyone and I hope we get over this soon. 🙏

Thanks for reading! Be sure to clap below and recommend this article if you liked it.

Reach me out on Twitter, Linkedin

--

--