需求:从FirstActivity跳到SecondActivity,在SecondActivity中进行了操作并返回到FirstActivity。
FirstActivity中的主要代码:
private static final int REQUEST_CODE = 1;
private static final int RESULT_CODE = 101;
//触发事件
public void gotoSencond(){
Intent intent=new Intent(this,SecondActivity.class);
startActivityForResult(intent, REQUEST_CODE);
}
//onActivityResult()方法中的第二个参数就是SecondActivity传回来的键,第三个参数就是SecondActivity传回来的键对应的值。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode==REQUEST_CODE) {
if(resultCode==RESULT_CODE) {
String result=data.getStringExtra("second");//取得SecondActivity返回的内容
textView.setText(result);
}
}
super.onActivityResult(requestCode, resultCode, data);
}
SecondActivity 类中的代码:
final int RESULT_CODE=101;
public void closeButton(View view) {
Intent intent=new Intent();
intent.putExtra("second", "I am second!");
setResult(RESULT_CODE, intent);
finish();
}