startActivityForResult
. When second activity is finish, it will send back result (some data) to main activity.How main activity get the result ?
From your
FirstActivity
call the SecondActivity
using startActivityForResult()
method Intent i = new Intent(this, SecondActivity.class);
startActivityForResult(i, 1);
In your
SecondActivity
set the data which you want to return back to FirstActivity
. If you don't want to return back, don't set any.
For example: In secondActivity if you want to send back data:
If you don't want to return data:
Now in your FirstActivity class override
reference : http://stackoverflow.com/questions/10407159/how-to-manage-start-activity-for-result-on-android
Intent returnIntent = new Intent();
returnIntent.putExtra("result",result);
setResult(RESULT_OK,returnIntent);
finish();
If you don't want to return data:
Intent returnIntent = new Intent();
setResult(RESULT_CANCELED, returnIntent);
finish();
Now in your FirstActivity class override
onActivityResult()
method. And process intent data by resultCode. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if(resultCode == RESULT_OK){
String result=data.getStringExtra("result");
}
if (resultCode == RESULT_CANCELED) {
//Write your code if there's no result
}
}
}
reference : http://stackoverflow.com/questions/10407159/how-to-manage-start-activity-for-result-on-android