注意: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 文档写得很清楚,
public final void setResult (int resultCode, Intent data)
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
resultCode | The result code to propagate back to the originating activity, often RESULT_CANCELED or RESULT_OK |
---|---|
data | The data to propagate back to the originating activity. |
从上可知:resultCode 应该设置为Activity.RESULT_OK,也即-1