没有历史和儿童活动的结果

时间:2011-05-11 09:45:37

标签: android

我有以下情况:

活动A - > 活动B (noHistory set) - > 活动C

当C完成时,我需要将 C 的结果返回到 A 。有可能吗?

我能实现这一目标的唯一方法是保留B的历史记录并通过它转发C的结果。因此,如果B启动并且程序从后台恢复,则将显示B.我希望B不会被保留在堆栈中。

2 个答案:

答案 0 :(得分:3)

其他详情

enter image description here

在活动A:

Intent intent = new Intent(getActivity(), B.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivityForResult(intent, REQUEST_CODE);

在活动A:

public void onActivityResult(int requestCode, int resultCode, Intent data) {
   if (resultCode == Activity.RESULT_OK && requestCode == REQUEST_CODE) {
      // TODO
   }
}

在活动B中:

Intent intent = new Intent(getActivity(), C.class);
intent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
startActivity(intent);

在活动C中:

Intent intent = new Intent();
// put something to intent
setResult(Activity.RESULT_OK, intent);
finish();

答案 1 :(得分:2)

正如Kamen已经证实的那样,在这种情况下应该使用FLAG_ACTIVITY_FORWARD_RESULT。正如教程所说:

If set and this intent is being used to launch a new activity from an existing one, then the reply target of the existing activity will be transfered to the new activity. This way the new activity can call setResult(int) and have that result sent back to the reply target of the original activity.