我从activityA调用activityB并使用intent传递一个类对象。
Intent intentB = new Intent(v.getContext(), activityB.class);
intentB.putExtra("data", data); //data is a class object
startActivity(intentB);
activityB启动正常,我也能够提取数据。在activityB中,我正在修改数据对象。我想在调用activityB.onDestroy()时将这个修改过的数据对象发送回activityA。
有什么建议吗?
这是我的代码:
activityA,开始Intent
:
Intent i = new Intent(this, activityB.class);
i.putExtra("object", Class.object);
startActivityForResult(i, 1);
activityA,抓住意图:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
object = data.getParcelableExtra("object"); //the Class implements Parcelable
}
}
activityB
protected void onDestroy() {
Intent data = new Intent();
setResult(RESULT_OK, data)
finish(); // ends current activity
}
答案 0 :(得分:9)
利用ActivityResult。
startActivityForResult(int, Intent)
setResult(RESULT_OK, data)
(其中数据是意图)onActivityResult(int, int, Intent)
并抓住data
- 意图活动A,开始Intent
:
Intent i = new Intent(this, SecondActivity.class);
startActivityForResult(i, 1);
活动A,捕获数据:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
// make use of "data" = profit
}
}
活动B:
Intent data = new Intent();
setResult(RESULT_OK, data)
finish(); // ends current activity