安卓:请求code和结果code [英] Android: requestCode and resultCode

查看:159
本文介绍了安卓:请求code和结果code的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我,如果我理解申请code和结果$ C $的概念,不知道c正确?基本上,我有与活动相关的任意整数(请求code)。例如,在记事本教程,我们有

 私有静态最终诠释ACTIVITY_CREATE = 0;
私有静态最终诠释ACTIVITY_EDIT = 1;

我们再使用startActivityforResult(意向,要求code)启动一项活动,例如创造记活动。我们做一下该活动并返回结果code。

在父活动,我们检测的结果code用的onActivityResult(要求code,结果code,意图)。然后我们可以使用请求code看到的是正在返回其活性,结果code看到活动的结果,和意图的额外得到返回的数据。


这有什么特别的内置结果codeS,喜欢RESULT_CANCELED?在开发者网站的文档似乎表明,内置在结果仅仅是整数。

和在我看来,这可能会带来麻烦?举例来说,如果我有10个可能的活动推出,那么我必须有一个巨人的onActivityResult函数检查返回该活动的,是不是?


解决方案

  

这有什么特别的内置结果codeS一样,
  RESULT_CANCELED?在开发者网站的文件似乎
  表明,内置的结果是简单的整数。


是的。这些codeS是标准的反应。例如,如果一个活动你开始返回 RESULT_CANCELED (尤其是操作系统的活动或标准的应用程序,如摄像头),这意味着该活动被取消。这些标准的结果应根据文档PTED间$ P $。事实上,你的活动应该使用这些标准的结果(以便其他应用程序开发者开始你的活动更轻松地),除非有一个强有力的理由不这样做。


  

和在我看来,这可能会带来麻烦?例如,如果我
  有10种可能的活动推出,那么我必须有一个巨大的
  的onActivityResult函数检查返回该活动的,
  是不是?


是的。使用switch语句来处理这些。

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?

解决方案

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.

这篇关于安卓:请求code和结果code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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