Dear Readers,
In this tutorial we are going to learn about how to get the result back from an activity that we launched, this is often the use case when you want to know the operation performed by the user on another activity that is started. The simplest example could be like you want to know the text the user typed in another activity, this is a use case of sending data within your application whereas you can also start other applications for the result, for example, you want the user to select a photo from his gallery which will discuss in a new post, in this tutorial we will fully focus on explaining how to start an activity which will return a result once it is finished, If you want to know how to start an activity in android with kotlin or send data to another activity you can refer the below posts
How to pass data between activities in android with kotlin
Learn How to Store Data In Android With Shared Preference In Kotlin
How to start Activity and get results back :
Let's say you have two activity in your application, and you want the result back, which could be anything from an activity to the one which started it. Initially, we would start the new activity by calling the startActivityForResult function which in later once the second activity's finish is called it will go back to the first activity then onActivityResult callback will be called with request code, result code, and the data which is passed from second activity, But recently we came to know when updating our activity to the latest version that startActivityForResult result has been deprecated along with onActivityResult which is also deprecated, since the main bridge to the complete setup to get back result from another activity has been deprecated so what is the alternative for it? , we found there are new APIs that are provided as a replacement for the deprecated startActivityForResult and onActivityResult, which is much useful when compared to the previous onActivityResult, In order to use the new ActivityResult APIs, we have to update our activity version to the current stable version "1.2.3" (as of 17-May-2021) additionally always check the latest version here Activity Releases. Add the below dependency to your app-level build Gradle
def activity_version = "1.2.3"
implementation "androidx.activity:activity-ktx:$activity_version"
IMPORTANT: When updating your activity use must update the fragment as well to 1.3.0, so along with the above add the fragment as well in your build Gradle.
def fragment_version = "1.3.0"
implementation "androidx.fragment:fragment-ktx:$fragment_version"
How to use ActivityResult API to get the result from activity:
Before this API we used startActivityForResult along with onActivityResult call back to get the result from another activity, but both startActivityForResult and onActivityResult were deprecated, and using a deprecated API is not a good practice as it might be removed from the code base in near future its is best to keep your code base updated with this new APIs provided to achieve the same functionality without a sweat, Yes we know that updating an existing codebase could be a headache because there might be a missing functionality in the new API, but in this case its actually a good things because these new APIs use the same kind of pattern to achieve the result, so traditionally what we do is we start a new activity with startActivityForResult to get the result from another activity instead of a simple startActivity, and once that new activity is finished we would get the result in the onActivityResult callback, Likewise we are going to do the same here, where we register a callback to get result that call back will give us launcher to start the activity, see simple as it is, see below for the sample implemetaion.
Register callback to get result:
First, we need to register a callback to get the result, we can do that by registering registerForActivityResult()
which takes two parameters one is ActivityResultContract
and an ActivityResultCallback
and returns an ActivityResultLauncher
.
You can refer to the below code for example:
private val secondActivityWithResult =
registerForActivityResult(ActivityResultContracts.StartActivityForResult())
{ result ->
if (result.isResultOk()){
if (result.data?.hasExtra(RESULT_TEXT)!!){
resultTextView.text = result.data!!.extras?.getString(RESULT_TEXT)
?: "No Result Provided"
}
}
}
Once, you have registered the callback you can use it to launch the new activity, as this will return an ActivityResultLauncher, then call secondActivityWithResult.launch() which will accept an Intent parameter in which we define the activity which has to be started.
You Might Be interested:
Android ViewPage2 Kotlin sample code
If you are familiar with the onActivityResult callback or have any previous implementation of it, you can see that the code inside the above block here should be present in the onActivityResult callback. And instead of startActivityForResult, we call launch.
You can refer to the below code for example:
getResultButton.setOnClickListener {
secondActivityWithResult.launch(Intent(this,SecondActivity::class.java))
}
It is as same as we start a new activity instead of just calling the startActivity, we are using the ActivityResultLauncher to get the result from the started activity.
Set result before finish :
val intent = Intent()
intent.putExtra(RESULT_TEXT,getString(R.string.the_result_string,resultEditText.text))
setResult(Activity.RESULT_OK,intent)
0 Comments
Have doubts? Shoot your thoughts....