MongoDB Developer
Kotlin
plus
Sign in to follow topics
MongoDB Developer Centerchevron-right
Developer Topicschevron-right
Languageschevron-right
Kotlinchevron-right

StartActivityForResult is Deprecated!

Mohit Sharma2 min read • Published Sep 14, 2021 • Updated May 12, 2022
Kotlin
Facebook Icontwitter iconlinkedin icon
Rate this article
star-empty
star-empty
star-empty
star-empty
star-empty

Introduction

Android has been on the edge of evolution for a while recently, with updates to androidx.activity:activity-ktx 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:
New Way:
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:

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: 😄
With the new API, this is possible using ActivityResultRegistry directly.

Summary

I have found the registerForActivityResult useful and clean. Some of the pros, 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.
Hope this was informative and enjoyed reading it.

Facebook Icontwitter iconlinkedin icon
Rate this article
star-empty
star-empty
star-empty
star-empty
star-empty
Related
Tutorial

Serverless Development with Kotlin, AWS Lambda, and MongoDB Atlas


Aug 01, 2023 | 6 min read
Article

Migrating Android Apps from Realm Java SDK to Kotlin SDK


Sep 05, 2023 | 10 min read
Tutorial

Building an Android App


Jan 22, 2024 | 6 min read
Article

Realm Meetup - Realm Kotlin Multiplatform for Modern Mobile Apps


Mar 21, 2023 | 23 min read
Table of Contents