Activity_A:
1、使用startActivityForResult(Intent intent, int requestCode) 方法打开Activity;
Intent nextIntent =new Intent(this, TwoActivity.class);
Bundle bd=new Bundle();
bd.putString("name", "xioaming");
bd.putInt("age",25);
nextIntent.putExtra("bundle", bd);
startActivityForResult(nextIntent, 100);
2、 重写onActivityResult(int requestCode, int resultCode, Intent data) 方法;
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if(data==null){
return;
}
String name=data.getStringExtra("name");
System.out.println(resultCode+"返回的结果为"+name);
}
Activity_B
3、新Activity中调用setResult(int resultCode, Intent data) 设置返回数据之后,关闭Activity就会调用上面的onActivityResult方法;
private void returnResult() {
Intent intent=new Intent();
intent.putExtra("name", "xiaohong");
setResult(100, intent);
}