Using startActivityForResult () is when you want one action to start another action, but then the second action returns some result to the first when it ends. So, an example would be Activity A starting Activity B, and then when Activity B finishes, it will send a response back to Activity A. So, to achieve this, several steps must be taken.
First you need to start the second activity ...
Intent intent = new Intent(this, YourSecondActivity.class); Bundle bundle = new Bundle(); bundle.putString("yourStringExtra", theStringExtra); intent.putExtras(bundle); startActivityForResult(intent, 1);
Then, when you are ready to complete the second action, you need to set the result to return to the first operation. You do it like this ...
Intent intent = new Intent(); intent.putExtra("string_result_from_second_activity", stringResult); setResult(RESULT_OK, intent); finish();
Then, after completing the second action, the first action is restarted, and you can intercept the result from the second action by overriding onActivityResult (), like this ...
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if(requestCode == 1) { if(resultCode == RESULT_OK) { String resultString = data.getStringExtra("string_result_from_second_activity"); } } }
So, in this example, the second action sends the stringResult string back to the first activity when it ends with RESULT_OK resultCode. That way, you check requestCode (which we set to β1β when we call startActivityForResult () in the first step), then make sure resultCode is set to RESULT_OK , and then we go ahead and access a line other than Intent.
Hope this clarifies the situation!