我是否需要明确的setResult到RESULT_CANCELED? [英] Do I need to explicitly setResult to RESULT_CANCELED?

查看:932
本文介绍了我是否需要明确的setResult到RESULT_CANCELED?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个希望用户选择一个项目的活动,但用户也可以选择单击后退按钮,而不选择一个项目。这种项目选择活动开始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?

推荐答案

在<一个href="http://developer.android.com/reference/android/app/Activity.html#StartingActivities">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.

这篇关于我是否需要明确的setResult到RESULT_CANCELED?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆