The default value for resultCode when activity ends

This may be a basic question, but I hope to get some clarity.

What I'm trying to do: 1) Starting an operation using requestCode and processing two operations in onActivityResult, one uses RESULT_OK, the other uses RESULT_CANCELLED. I clearly indicate each of them.

The problem even is that I close the activity only with the "Back" button and without setting any result, the "Activity" activity in the back stack gets RESULT_CANCELLED.

After going through the source code, I see that RESULT_CANCELLED is the default value for the result code and that the return code is always sent back.

enter image description here

enter image description here

I read this right, and this is what happens all the time? or am i doing something wrong in my application?

: https://github.com/android/platform_frameworks_base/blob/master/core/java/android/app/Activity.java

+4
2

, onActivityResult (..) onResume(). check Doc for Activity

resultCode - RESULT_CANCELLED.

setResult (int) , resultCode.

resultCode == RESULT_OK onActivityResult. onActivityResult , startActivityForResult.

, .

  @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // Check which request we're responding to
    if (requestCode == YOUR_REQUEST) {
        // Make sure the request was successful
        if (resultCode == RESULT_OK) {
            // so some work
        }
    }
}
+5

setResult()

+2

Source: https://habr.com/ru/post/1616825/


All Articles