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的基本用法,可以根據具體需求進行適當的擴展和處理。