Android: how to make an activity return results to the activity which calls it?
In order to start an activity which should return result to the calling activity, you should do something like below. You should pass the requestcode as shown below in order to identify that you got the result from the activity you started.
startActivityForResult(new Intent(“YourFullyQualifiedClassName”),requestCode);
In the activity you can make use of setData()
to return result.
Intent data = new Intent();
String text = "Result to be returned...."
//---set the data to pass back---
data.setData(Uri.parse(text));
setResult(RESULT_OK, data);
//---close the activity---
finish();
So then again in the first activity you write the below code in onActivityResult()
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == request_Code) {
if (resultCode == RESULT_OK) {
String returnedResult = data.getData().toString();
// OR
// String returnedResult = data.getDataString();
}
}
}
EDIT based on your comment: If you want to return three strings, then follow this by making use of key/value pairs with intent instead of using Uri.
Intent data = new Intent();
data.putExtra("streetkey","streetname");
data.putExtra("citykey","cityname");
data.putExtra("homekey","homename");
setResult(RESULT_OK,data);
finish();
Get them in onActivityResult like below:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == request_Code) {
if (resultCode == RESULT_OK) {
String street = data.getStringExtra("streetkey");
String city = data.getStringExtra("citykey");
String home = data.getStringExtra("homekey");
}
}
}
If you want to finish and just add a resultCode
(without data), you can call setResult(int resultCode)
before finish()
.
For example:
...
if (everything_OK) {
setResult(Activity.RESULT_OK); // OK! (use whatever code you want)
finish();
}
else {
setResult(Activity.RESULT_CANCELED); // some error ...
finish();
}
...
Then in your calling activity, check the resultCode
, to see if we're OK.
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == someCustomRequestCode) {
if (resultCode == Activity.RESULT_OK) {
// OK!
}
else if (resultCode = Activity.RESULT_CANCELED) {
// something went wrong :-(
}
}
}
Don't forget to call the activity with startActivityForResult(intent, someCustomRequestCode)
.
UPDATE Feb. 2021
As in Activity v1.2.0 and Fragment v1.3.0, the new Activity Result APIs
have been introduced.
The Activity Result APIs provide components for registering for a result, launching the result, and handling the result once it is dispatched by the system.
So there is no need of using startActivityForResult
and onActivityResult
anymore.
In order to use the new API, you need to create an ActivityResultLauncher in your origin Activity, specifying the callback that will be run when the destination Activity finishes and returns the desired data:
private val intentLauncher =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode == Activity.RESULT_OK) {
result.data?.getStringExtra("streetkey")
result.data?.getStringExtra("citykey")
result.data?.getStringExtra("homekey")
}
}
and then, launching your intent whenever you need to:
intentLauncher.launch(Intent(this, YourActivity::class.java))
And to return data from the destination Activity, you just have to add an intent with the values to return to the setResult()
method:
val data = Intent()
data.putExtra("streetkey", "streetname")
data.putExtra("citykey", "cityname")
data.putExtra("homekey", "homename")
setResult(Activity.RESULT_OK, data)
finish()
For any additional information, please refer to Android Documentation