Hi There,
Well, In a recent article we have shared a bit about the new Android activity result API, since the startActivityForResult and onActivityResult are deprecated, we have some new API's to work with to achieve the same functionality, the idea is simple we start a new activity and do some operation and get the result back from the activity, for this we used the famous startActivityForResult and onActivityResult, but both are deprecated in the updated Activity class so we continued to explore and found the Activity Result API which is a better replacement compared to the previous implementation as it verifies type-safe and reduce the boilerplate code and improves the implementation to look more readable.
How to get android activity result using ActivityResult API:
ActivityResult API Custom Contract:
A contract specifying that an activity can be called with an input of type I and produce an output of type O
Well that put things in perspective, right? Generally, we start an activity with an input that we want to pass on to the next activity and we will get back the result as an output with this it will make sure the input and output are type-safe and a contract will be a new class which makes the code less complex, Let's start with creating a custom contract class and we name it CustomContractParcelable. Why parcel able because that way only we can pass on data class in kotlin you can read Parcelable About Here
CustomContractParcelable.kt
class CustomContractParcelable<T : Parcelable?> : ActivityResultContract<Intent,T>(){
override fun createIntent(context: Context, inputAsIntent: Intent?): Intent {
return inputAsIntent!!
}
override fun parseResult(resultCode: Int, intent: Intent?): T? {
return if (resultCode == Activity.RESULT_OK){
return intent?.getParcelableExtra<T>(PARCELABLE_RESULT)
}else{
null
}
}
}
Above is the sample code for a custom contract for ActivityResultContract, First, we extend the ActivityResultContract which requires two type parameters an Input and Output.
This class must override the above two methods which are createIntent(context: Context, inputAsIntent: Intent) in here the second parameter is actually the input type parameter we mentioned in ActivityResultContract the input type could be anything so to make our contract more accessible and make it able to start more than one activity we are providing the intent itself as an input, the second method that overrides is parseResult(resultCode: Int, intent: Intent?) once the started activity finishes this method will be called and it will return the data of type we mentioned in the output parameter which makes this implementation type-safe and we can check the result code as well in here to pass the result back to our call back.
Use Custom Contract With ActivityResult API to get result:
private val secondActivityWithResult =
registerForActivityResult(CustomContractParcelable<SampleModel>()) { result ->
if (result != null) {
resultTextView.text = result.description
}
}
getResultButton.setOnClickListener {
secondActivityWithResult.launch(Intent(requireContext(), SecondActivity::class.java))
}
0 Comments
Have doubts? Shoot your thoughts....