StartActivityForResult is the Core/OG Part of the Android Task Ecosystem of the way we used to begin an task & get effects from it with this getting deprecated we now have were given a brand new & higher means RegisterActivityForResult.
Why used to be StartActivityForResult deprecated?
StartActivityForResult is essentially the most elementary part of the Android ecosystem however with its simple-to-use merit it had many disadvantages like
- Laborious to seek out from the place a undeniable request is being made
- Needed to ship a singular request code in case there’s a reproduction used. that can result in buggy effects occasionally
- The result’s ignored if the part is recreated
- onActivityResult callback doesn’t paintings smartly with fragments
Why new Task Result’s higher?
- Easy, simple & blank
- In-built & Customized Contract-based Reinforce
- Separate callback for each and every task (previous for each and every we used to have a not unusual task Consequence)
The best way to use Task Consequence API
Step 1: Claim the launcher
lateinit var launcher : ActivityResultLauncher
Step 2: Check in it
launcher = registerForActivityResult
(ActivityResultContracts.StartActivityForResult()) { consequence ->
useTheResult(consequence)
}
Notice: You should name registerForActivityResult()
earlier than the fragment or task is created; you can not release the ActivityResultLauncher
till the fragment or task’s Lifecycle
has reached CREATED
.
Step 3: Use the launcher
launcher.release(Intent(context, ResultActivity::elegance.java)
.putExtra(ResultActivity.PARAM_DATA,identity))
Now on this means every time we will be able to simply & successfully refactor all of the previous startActivityForResult()
One of the crucial Integrated ActivityResultContracts are :
- StartActivityForResult
- RequestPermission
- TakePicture
- CaptureVideo
- OpenDocument
You can to find extra right here: https://developer.android.com/reference/androidx/task/consequence/contract/package-summary
Together with those in-built consequence kind contracts, there may be reinforce for customized contracts i.e
Some Further ideas?
With Customized Contract we will be able to separate all of the common sense to the Contract Elegance
Even though this can be a a lot better means as in comparison to what we now have previous nonetheless that is coupled with our task.
With the intention to decouple it a little bit, we will be able to employ customization with are living information & lifecycle observer.
Additional studying
⨭ Getting a consequence from an task | Authentic Documentation
⨭ Introducing the Task Effects APIs | Adam Bennett
Catch me on Twitter: https://twitter.com/its_pra_tick