11

I'm wondering if I am understanding the concepts of requestCode and resultCode properly? Basically, I have an arbitrary integer (the requestCode) associated with an activity. For example, in the Notepad tutorial, we have

private static final int ACTIVITY_CREATE=0;
private static final int ACTIVITY_EDIT=1;

We then use startActivityforResult(intent, requestCode) to start an activity, e.g. the "create note" activity. We do something in that activity and return a resultCode.

In the parent activity we detect the resultCode with onActivityResult(requestCode, resultCode, intent). We can then use the requestCode to see which activity is being returned, the resultCode to see the result of that activity, and the intent's "extras" to get returned data.


Is there anything special about the built-in resultCodes, like RESULT_CANCELED? The documentation on the developer site seems to suggest that the built-in results are simply integers.

And it seems to me that this could get really messy? For example, if I had 10 possible activities to launch, then I'd have to have a giant onActivityResult function to check which activity's being returned, wouldn't I?

Machavity
31.8k27 gold badges97 silver badges108 bronze badges
asked Aug 8, 2011 at 17:19

2 Answers 2

12

Is there anything special about the built-in resultCodes, like RESULT_CANCELED? The documentation on the developer site seems to suggest that the built-in results are simply integers.

Yes. These codes are "standard" responses. For example, if an activity you started returns RESULT_CANCELED (in particular, an OS activity or a standard app such as the Camera), that means the activity was cancelled. These standard results should be interpreted according to the documentation. In fact, your activities should use these standard results (so that other app developers have an easier time starting your activities) unless there is a strong reason not to do so.

And it seems to me that this could get really messy? For example, if I had 10 possible activities to launch, then I'd have to have a giant onActivityResult function to check which activity's being returned, wouldn't I?

Yes. Use a switch statement to handle these.

answered Aug 8, 2011 at 17:24
Sign up to request clarification or add additional context in comments.

1 Comment

If you have to use more than the two standard result codes, start your first custom result code with Activity.RESULT_FIRST_USER, i.e. static final int MY_RESULT = Activity.RESULT_FIRST_USER and then static final int MY_OTHER_RESULT = MY_RESULT + 1
1

There is nothing special about the integers -- those constants are simply a convention. Your onActivityResult could be a single switch statement that dispatches results to methods to deal with them... no need for it to be giant. One should, of course, be using symbolic constants, as it appears you are.

answered Aug 8, 2011 at 17:24

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.