Android Intent 传值 --onActivityResult

来源:互联网 发布:nginx 域名访问 编辑:程序博客网 时间:2024/05/24 07:13

注意: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 Declarationvoid 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 haveIntent.FLAG_GRANT_READ_URI_PERMISSION and/orIntent.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_OKdataThe data to propagate back to the originating activity.
See Also
  • RESULT_CANCELED
  • RESULT_OK
  • RESULT_FIRST_USER
  • setResult(int)

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




0 0
原创粉丝点击