Android Intent 传值 --onActivityResult

注意:startActivityForResult传值方式入下:

            setResult(Activity.RESULT_OK,      intent);


Android 通过Intent传值有2种  一种是调转后传值,另一种是用startActivityForResult(intent, ATPERSON);来传值

Intent 传值很简单,存入后,在跳转后的页面获取即可。


    intent.putExtra("type", "send_weibo");

    type = getIntent().getStringExtra("type");


startActivityForResult多几句代码:

首先启动方式如下:

intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(intent, CAMERA);

其次,重写onActivityResult方法:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

}


然后,进行数据处理:

if (resultCode == Activity.RESULT_OK) {
            if (data != null) {
                if (requestCode == CAMERA)

}


如果Intent是跳转到其他Intent处理的,用如下方式设置传回:

    intent.putExtra("result", result);
                    PersonDetailActivity.this.setResult(Activity.RESULT_OK,
                            intent);// 设置返回数据
                    PersonDetailActivity.this.finish();


注意:setResult的第一个参数,网上很多博客写的都是错的,需要自己看方法声明,Android 文档写得很清楚,

Open Declaration void android.app. Activity.setResult( int resultCode, Intent data)

public final void setResult (int resultCode, Intent data)
Added in API level 1

Call this to set the result that your activity will return to its caller.

As of GINGERBREAD, the Intent you supply here can have Intent.FLAG_GRANT_READ_URI_PERMISSION and/or Intent.FLAG_GRANT_WRITE_URI_PERMISSION set. This will grant the Activity receiving the result access to the specific URIs in the Intent. Access will remain until the Activity has finished (it will remain across the hosting process being killed and other temporary destruction) and will be added to any existing set of URI permissions it already holds.

Parameters
resultCodeThe result code to propagate back to the originating activity, often RESULT_CANCELED or RESULT_OK
dataThe data to propagate back to the originating activity.

从上可知:resultCode 应该设置为Activity.RESULT_OK,也即-1




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值