I’m seeing inconsistent behaviour when calling setResult() and finish() on some activities that were started using startActivityForResult(). From other questions on here, it looks like a variety of reasons for unexpected behaviour are possible. Which variables do I need to look at when debugging for a complete picture of what is happening during the process of starting an activity for result and sending it back, given that the Activity may start Activities for result itself?
Specifically, I’d like to know how to see:
- How the result will be treated when it gets back to the starting activity
- Where (and if) a result will be sent when finish() is called
- What the currently set result is
I’m already watching:
mResultCode
mResultData
mParent
But they don’t give enough information their own. I’d like the entire state of my application’s result mechanism.
request code …
when you call startActivityForResult(), you supply a request code. the request code allows you to match up a specific activity result with the particular start request. specifically, in onActivityResult(), you will be passed a request code that allows you to make that comparison.
result code …
the result code is just a way of doing coarse-grained messaging back from the started activity to the starting activity. that is, the started activity can set a result code by calling setResult(int) which is returned to the starting activity in onActivityResult(). again, this is typically used to perform coarse grained messaging like SUCCESS or FAIL.
result data …
finally, to pass finer grained data back, use the variant of setResult(int, Intent) that takes both the result code and an intent. when you construct the intent, add whatever extras you wish to pass back to the starting activity.