我是否需要明确的setResult到RESULT_CANCELED?明确、setResult、RESULT_CANCELED

2023-09-12 05:27:23 作者:谈笑风生不动情

我有一个希望用户选择一个项目的活动,但用户也可以选择单击后退按钮,而不选择一个项目。这种项目选择活动开始startActivityForResult()。

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().

我想知道两件事情。首先,是它足以只的setResult(RESULT_OK)时,该项目实际选择,例如,我可以依靠的结果没有被设置为RESULT_OK如果我不设置明确?

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?

第二,如果这还不足以依靠(初始化)的结果,是不是还好之后调用的setResult()多次,第一次初始化的结果RESULT_CANCELED,然后调用的setResult(RESULT_OK)用户选择一个项目,完成活动之前?

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?

推荐答案

在documentation:

如果一个子活动因故失败(如崩溃),该   父活动将获得的结果与code    RESULT_CANCELED

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

所以,结果已是 RESULT_CANCELED ,如果 你没有明确指定其他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.

 
精彩推荐