Run action:
you need to pass an additional integer argument to the startActivityForResult () method. You can do this by specifying a constant or just put an integer. The integer argument is the "request code" that identifies your request. When you get an Intent result, the callback provides the same request code so that your application can correctly identify the result and determine how to handle it.
static final int ASK_QUESTION_REQUEST = 1; // Create an Intent to start SecondActivity Intent askIntent = new Intent(FirstActivity.this, SecondActivity.class); // Start SecondActivity with the request code startActivityForResult(askIntent, ASK_QUESTION_REQUEST);
Return Result:
After completing your work in the second class of activity, simply set the result and call this activity where it came from, and finally, do not forget to write the finish () statement.
// Add the required data to be returned to the FirstActivity sendIntent.putExtra(Result_DATA, "Your Data"); // Set the resultCode to Activity.RESULT_OK to // indicate a success and attach the Intent // which contains our result data setResult(RESULT_OK, sendIntent); // With finish() we close the SecondActivity to // return to FirstActivity finish();
Get the result:
When you are done with follow-up and return, the system calls your onActivityResult () activity. This method includes three arguments:
@ The request code that you passed to startActivityForResult (). @ The result code indicated by the second action. This is either RESULT_OK if the operation was successful, or RESULT_CANCELED if the operation was not completed @Intent, which carries the result data.
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); // check if the request code is same as what is passed here it is 1 if (requestCode == ASK_QUESTION_REQUEST) { // Make sure the request was successful if (resultCode == RESULT_OK) { final String result = data.getStringExtra(SecondActivity.Result_DATA); // Use the data - in this case display it in a Toast. Toast.makeText(this, "Result: " + result, Toast.LENGTH_LONG).show(); } } }
For more details see this demo. Getting the result from Activity.
Zakir hossain
source share