HomeLearnArticleStartActivityForResult is deprecated !!

StartActivityForResult is deprecated !!

Updated: Sep 14, 2021 |

Published: Sep 14, 2021

  • Kotlin
  • Android
  • Mobile

By Mohit Sharma

Rate this article

#Introduction

Android has been on the edge of evolution for a while recently, with updates to androidx.activity to 1.2.0. It has deprecated startActivityForResult in favour of registerForActivityResult.

It was one of the first fundamentals that any Android developer has learned, and the backbone of Android's way of communicating between two components. API design was simple enough to get started quickly but had its cons, like how it’s hard to find the caller in real-world applications (except for cmd+F in the project 😂), getting results on the fragment, results missed if the component is recreated, conflicts with the same request code, etc.

Let’s try to understand how to use the new API with a few examples.

Example 1: Activity A calls Activity B for the result

Old School:

1// Caller
2val intent = Intent(*context*, Activity1::class.java)
3startActivityForResult(intent,REQUEST_CODE)
4
5// Receiver
6override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
7 super.onActivityResult(requestCode, resultCode, data)
8 if(resultCode == Activity.RESULT_OK && requestCode == REQUEST_CODE){
9 val value = data?.getStringExtra("input")
10 }
11}

New Way:

1// Caller
2val intent = Intent(*context*, Activity1::class.*java*)
3getResult.launch(intent)
4
5// Receiver
6private val getResult =
7 registerForActivityResult(
8 ActivityResultContracts.StartActivityForResult()) {
9 if(it.resultCode == Activity.RESULT_OK){
10 val value = it.data?.getStringExtra("input")
11 }
12 }

As you would have noticed, registerForActivityResult takes two parameters. The first defines the type of action/interaction needed (ActivityResultContracts) and the second is a callback function where we receive the result.

Nothing much has changed, right? Let’s check another example.

Example 2: Start external component like the camera to get the image:

1//Caller
2getPreviewImage.launch(null)
3
4//Receiver
5private val getPreviewImage =
6 registerForActivityResult(ActivityResultContracts.TakePicture(){bitmap ->
7 // we get bitmap as result directly
8
9}

The above snippet is the complete code getting a preview image from the camera. No need for permission request code, as this is taken care of automatically for us!

Another benefit of using the new API is that it forces developers to use the right contract. For example, with ActivityResultContracts.TakePicture() — which returns the full image — you need to pass a URI as a parameter to launch, which reduces the development time and chance of errors.

Other default contracts available can be found here.

Example 3: Fragment A calls Activity B for the result

This has been another issue with the old system, with no clean implementation available, but the new API works consistently across activities and fragments. Therefore, we refer and add the snippet from example 1 to our fragments.

Example 4: Receive the result in a non-Android class

Old Way: 😄

https://mongodb-devhub-cms.s3.us-west-1.amazonaws.com/giphy_f99f638e0c.webp

With the new API, this is possible using ActivityResultRegistry directly.

1class MyLifecycleObserver(private val registry : ActivityResultRegistry): DefaultLifecycleObserver {
2
3 lateinit var getContent : ActivityResultLauncher<String>
4
5 override fun onCreate(owner: LifecycleOwner) {
6 getContent = registry.register("key", owner, GetContent()) { uri ->
7 // Handle the returned Uri
8 }
9 }
10
11 fun selectImage() {
12 getContent.launch("image/*")
13 }
14}
15
16class MyFragment : Fragment() {
17 lateinit var observer : MyLifecycleObserver
18
19 override fun onCreate(savedInstanceState: Bundle?) {
20 // ...
21
22 observer = MyLifecycleObserver(requireActivity().activityResultRegistry)
23 lifecycle.addObserver(observer)
24 }
25
26 override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
27 val selectButton = view.findViewById<Button>(R.id.select_button)
28
29 selectButton.setOnClickListener {
30 // Open the activity to select an image
31 observer.selectImage()
32 }
33 }
34}

#Summary

I have found the registerForActivityResult useful and clean. Some of the cons, in my opinion, are:

  1. Improve the code readability, no need to remember to jump to onActivityResult() after startActivityForResult.
  2. ActivityResultLauncher returned from registerForActivityResult used to launch components, clearly defining the input parameter for desired results.
  3. Removed the boilerplate code for requesting permission from the user.
Rate this article
MongoDB Icon
  • Developer Hub
  • Documentation
  • University
  • Community Forums

© MongoDB, Inc.