startActivityForResult方法用于启动一个Activity并等待结果返回,其用法如下:
Intent intent = new Intent(this, SecondActivity.class);
startActivityForResult(intent, REQUEST_CODE);
Intent intent = new Intent();
intent.putExtra("result", "Hello");
setResult(RESULT_OK, intent);
finish();
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE) {
if (resultCode == RESULT_OK) {
String result = data.getStringExtra("result");
// 处理返回的结果
} else if (resultCode == RESULT_CANCELED) {
// 处理取消操作
}
}
}
以上是startActivityForResult的基本用法,可以根据具体需求进行适当的扩展和处理。