Blog Infos
Author
Published
Topics
Published
Topics

StartActivityForResult is the Core/OG Component of the Android Activity Ecosystem of how we used to start an activity & get results from it with this getting deprecated we have got a new & better way RegisterActivityForResult.

Why was StartActivityForResult deprecated?

StartActivityForResult is the most fundamental component of the Android ecosystem but with its simple-to-use advantage it had many disadvantages like

  • Hard to find from where a certain request is being made
  • Had to send a unique request code in case there is a duplicate used. that can lead to buggy results sometimes
  • The result is missed if the component is recreated
  • onActivityResult callback doesn’t work well with fragments
Why new Activity Result is better?
  • Simple, easy & clean
  • Inbuilt & Custom Contract-based Support
  • Separate callback for each activity (earlier for each we used to have a common activity Result)
How to use Activity Result API

Step 1: Declare the launcher

lateinit var launcher : ActivityResultLauncher

Step 2: Register it

launcher = registerForActivityResult
(ActivityResultContracts.StartActivityForResult()) { result ->
            useTheResult(result)
}

Note: You must call registerForActivityResult() before the fragment or activity is created; you cannot launch the ActivityResultLauncher until the fragment or activity’s Lifecycle has reached CREATED.

Step 3: Use the launcher

launcher.launch(Intent(context, ResultActivity::class.java)
.putExtra(ResultActivity.PARAM_DATA,id))

Now in this way whenever we can easily & efficiently refactor all the old startActivityForResult()

Some of the Built-in ActivityResultContracts are :

  • StartActivityForResult
  • RequestPermission
  • TakePicture
  • CaptureVideo
  • OpenDocument

You can find more here: https://developer.android.com/reference/androidx/activity

/result/contract/package-summary

Along with these inbuilt result type contracts, there is also support for custom contracts i.e

class CustomContract : ActivityResultContract<String, String?>() {
const val DATA = "data"
const val INPUT_DATA = "input_data"
override fun createIntent(context: Context, input: String?): Intent {
val intent = Intent(context, DummyResultActivity::class.java)
intent.putExtra(INPUT_DATA, input)
return intent
}
override fun parseResult(resultCode: Int, intent: Intent?): String? {
return when (resultCode) {
//Transforming our result to required format before returning it
Activity.RESULT_OK -> intent?.getStringExtra(DATA)
else -> null
}
}
}
Some Additional thoughts?

With Custom Contract we can separate all the logic to the Contract Class
Though this is a much better way as compared to what we have earlier still this is coupled with our activity.
In order to decouple it a bit, we can make use of customization with live data & lifecycle observer.

Further reading

⨭ Getting a result from an activity | Official Documentation
⨭ Introducing the Activity Results APIs | Adam Bennett

Catch me on Twitter: https://twitter.com/its_pra_tick

This article was originally published on proandroiddev.com on September 27, 2022

Job Offers

Job Offers


    Android Software Engineer (f/m/d)

    Paradox Cat GmbH
    Munich
    • Full Time
    apply now

    Mobile Engineer

    OLX Group
    Remote, Portugal, Spain, Romania, Poland
    • Full Time
    apply now

    Kotlin Multiplatform Mobile Developer

    Touchlab
    Remote
    • Full Time
    apply now
Load more listings

OUR VIDEO RECOMMENDATION

, ,

The power of Camera APIs and OpenGL all together

Are you using the best out of your device camera and GPU in your app? Have you ever wondered how a view or camera frame is drawn on the screen?
Watch Video

The power of Camera APIs and OpenGL all together

Suneet Agrawal
Engineering Manager
GamesKraft

The power of Camera APIs and OpenGL all together

Suneet Agrawal
Engineering Manager
GamesKraft

The power of Camera APIs and OpenGL all together

Suneet Agrawal
Engineering Manager
GamesKraft

Jobs

YOU MAY BE INTERESTED IN

YOU MAY BE INTERESTED IN

blog
This tutorial is the second part of the series. It’ll be focussed on developing…
READ MORE
blog
We recently faced a problem with our application getting updated and reaching slowly to…
READ MORE
blog
A few weeks ago I started with a simple question — how to work…
READ MORE
blog
One of the main functions of a mobile phone was to store contacts information.…
READ MORE

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Menu