Question

I've seen an answer that gets really close to solving my question: https://stackoverflow.com/a/13060588/1481760

I'm looking to create an ActivityInstrumentationTestCase2 that tests onActivityResult for various values of "requestCode".

Was it helpful?

Solution 4

My mistake... In my test case I forgot to trigger the code that actually uses startActivityForResult. I corrected that and as expected this caused onActivityResult to be called. Allowing me to test onActivityResult with various values of requestCode.

One tip- remember that the second parameter (the timeout) for waitForMonitorWithTimeout is in milliseconds not seconds as the documentation suggests.

OTHER TIPS

From Android Doc

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // Check which request it is that we're responding to (THIS IS WHAT YOU ARE LOOKING FOR)
    if (requestCode == PICK_CONTACT_REQUEST) {
        // Make sure the request was successful
        if (resultCode == RESULT_OK) {


        }
    }
}

When you use the below method.request code is userdefine you can specify here any number.

 startActivityForResult(i, requestCode);

and you receive the same requestCodeon the below method at argument requestCode.

protected void onActivityResult(int requestCode, int resultCode, Intent data) { }

You can use it like below,

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == Activity.RESULT_OK) {
            switch (requestCode) {
            case CAPTURE_AUDIO:
                 //your code for audio result
            break;

            case CAPTURE_VIDEO:
                // your code for video result
                break;

            default:
                break;
            }
        }
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top