On activity1 use the startActivityForResult
call to start the activity2 so you can get the result of it:
Intent i = new Intent(this, Activity2.class);
startActivityForResult(i, 1);
On activity2:
Intent i = new Intent(this, Activity3.class);
startActivityForResult(i, 2);
On activity3 at the point you set the result:
Intent returnIntent = new Intent();
returnIntent.putExtra("result",result);
setResult(Activity.RESULT_OK,returnIntent);
finish();
This sets the result and closes Activity3 with the call to finish
.
Now, on Activity2 you should add this code to get the result:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 2) {
if (resultCode == Activity.RESULT_OK){
String result = data.getStringExtra("result");
Intent returnIntent = new Intent();
returnIntent.putExtra("result", result); // send the result of Activity3
setResult(Activity.RESULT_OK,returnIntent);
finish();
}
if (resultCode == Activity.RESULT_CANCELED) {
//Write your code if there's no result
}
}
}
And you can get the result on Activity1:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if(resultCode == Activity.RESULT_OK){
String result = data.getStringExtra("result"); // Now you have the result here
}
if (resultCode == Activity.RESULT_CANCELED) {
//Write your code if there's no result
}
}
}
Notice you should get consistent with the integers you set on the startActivityForResult and the requestCode you receive, I recommend using constants here.
If you need more info have a look at https://developer.android.com/training/basics/intents/result
and How to manage startActivityForResult on Android?