Get result from started activity in Android
Published on 07.09.2014
Our scenario is:
FirstActivity call SecondActivity and when SecondActivity is finish his job we must pass some data to FirstActivity.
For this we are starting SecondActivity like that:
Intent i = new Intent(this, SecondActivity.class); startActivityForResult(i, 901);Before we "finish" SecondActivity we must set some data like
Intent returnIntent = new Intent(); returnIntent.putExtra("data",EXTRA_DATA_HERE); setResult(RESULT_OK,returnIntent); finish();
Or if we don't want to return data / just like if something is wrong
setResult(RESULT_CANCELED, new Intent()); finish();
And after that in our FirstActivity class we must write something like:
protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == 901) { if(resultCode == RESULT_OK){ String result=data.getStringExtra("data"); } if (resultCode == RESULT_CANCELED) { // we don't have data } } }
More information about Activities in Android can be found at Android developer documentation.