Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I need to explicitly setResult to RESULT_CANCELED?

I have an activity that expects the user to select an item, but the user can instead choose to click the back button without selecting an item. This "item selecting" activity is started with startActivityForResult().

I was wondering two things. First, is it sufficient to only setResult(RESULT_OK) when the item is actually selected, i.e., can I rely upon the result not being set to RESULT_OK if I don't set it explicitly?

Second, if this isn't sufficient to rely upon the (uninitialized) result, is it okay to call setResult() multiple times, the first time "initializing" the result to RESULT_CANCELED, and then calling SetResult(RESULT_OK) after the user selects an item and before finishing the activity?

like image 260
Jeff Axelrod Avatar asked Jul 03 '11 18:07

Jeff Axelrod


1 Answers

From the documentation:

If a child activity fails for any reason (such as crashing), the parent activity will receive a result with the code RESULT_CANCELED.

So the result is already RESULT_CANCELED, if you don't explicitly specify the other code. When the user exits your app via "back" button - you receive RESULT_CANCELED as well.

like image 114
Dirol Avatar answered Oct 14 '22 17:10

Dirol