79092556

Date: 2024-10-16 05:21:47
Score: 2.5
Natty:
Report link

Title: How to handle onActivityResult with unexpected positive result codes?

Body:

I am working on an Android app and encountered a situation where onActivityResult sometimes returns unexpected positive result codes instead of the usual RESULT_OK. I want to handle the RESULT_CANCELED case explicitly, and for any other result code (including RESULT_OK or other positive numbers), I want to handle it in the else block.

Here is the current implementation:

java Copy code

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_CODE_INITILIZATION) {
        if (resultCode == Activity.RESULT_OK) {  
            // handle result OK
        }
        if (resultCode == RESULT_CANCELED) {
            // handle result canceled
        }
    } else {
        super.onActivityResult(requestCode, resultCode, data);
    }
}
Instead of checking for RESULT_OK explicitly, I want to ensure that if the result

Code is not RESULT_CANCELED, the code should proceed with handling other positive result codes. Here’s what I’m thinking of doing:

java Copy code

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_CODE_INITILIZATION) {
        if (resultCode == RESULT_CANCELED) {
            // handle result canceled
        } else {
            // handle result OK and other positive result codes
        }
    } else {
        super.onActivityResult(requestCode, resultCode, data);
    }
}

The idea is that if the result code is RESULT_CANCELED, it will be handled separately, and for any other positive number (including RESULT_OK), it will go into the else block.

Is this the correct approach? Are there any potential issues I should be aware of when dealing with result codes other than RESULT_OK and RESULT_CANCELED?

Reasons:
  • RegEx Blacklisted phrase (1): I want
  • Long answer (-1):
  • Has code block (-0.5):
  • Ends in question mark (2):
  • Low reputation (1):
Posted by: Varun Kumar n m