startActivityForResult用法 从FirstActivity跳转到SecondActivity然后返回FirstActivity并且获取从SecondActivity

startActivityForResult主要用来从FirstActivity跳转到SecondActivity然后返回FirstActivity并且获取从SecondActivity传回来的参数。
使用方法:如下从ClockManagerActivity跳转到NewMapActivity并且传address值
ClockManagerActivity:

 Intent intent = new Intent(ClockManagerActivity.this,NewMapActivity.class);
 intent.putExtra("address",position);
 startActivityForResult(intent,3);

当请求码为3且结果码为RESULT_OK时,获取从NewMapActivity传过来的position值

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 3 && resultCode == RESULT_OK) {
// SearchAddressInfo info = (SearchAddressInfo) data.getParcelableExtra("position");
String position = data.getStringExtra("position");
mTvClockInAddress.setText(position);
}
}

NewMapActivity:
获取从ClockManagerActivity传过来的address值

String position = getIntent().getStringExtra("address");

设置resultCode码,传递position参数,关闭当前Activity

Intent intent = getIntent();
String s = addressInfo.addressName + addressInfo.title;
intent.putExtra("position", s);
setResult(RESULT_OK,intent);
finish();