Search code examples
androidandroid-activityonactivityresult

startActivityForResult and setResult with RESULT_OK does not work


I'm not sure if this is expected behavior, but if I do the following in OneActivity to launch TwoActivity:

Intent intent = new Intent(this, TwoActivity.class);
startActivityForResult(intent, RESULT_OK);

And in TwoActivity when I pass back to OneActivity:

Intent resultIntent = new Intent();
resultIntent.putExtra(SOURCE, TAG);
setResult(RESULT_OK, resultIntent);

finish();

With the above code and after overriding onActivityResult in OneActivity nothing happens. onActivityResult doesn't even seem to be called. If, however, I change RESULT_OK to 0, it works.

Is this expected? Has anyone else experienced that?


Solution

  • Check out the docs definition of the startActivityForResult method. It says:

    requestCode int: If >= 0, this code will be returned in onActivityResult() when the activity exits.

    So your request code should be >= 0. If you check the value of the RESULT_OK response code, it's -1. It's important to note that the request code isn't the same as the result code. The request code serves you to identify the request the result is for, and the result code tells you if the request was successful or not.