简体   繁体   English

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

[英]noHistory and child activity result

I have the following case: 我有以下情况:

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

I need to have a result from C returned to A when C finishes. 当C完成时,我需要从C返回到A的结果。 Is it possible? 可能吗?

The only way I could achieve this is by retaining the history for B and forwarding the result from C via it. 我能够实现这一目标的唯一方法是保留B的历史记录并通过它转发C的结果。 Thus, though, if B is launched and the program is restored from background, B will be displayed. 因此,如果B启动并且程序从后台恢复,则将显示B. I would prefer that B is not kept on the stack. 我希望B不会被保留在堆栈中。

Additional details 额外细节

在此输入图像描述

In activity A: 在活动A:

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

In activity A: 在活动A:

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

In activity B: 在活动B中:

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

In activity C: 在活动C中:

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

As already confirmed by Kamen, FLAG_ACTIVITY_FORWARD_RESULT should be used in such a case. 正如Kamen已经证实的那样,在这种情况下应该使用FLAG_ACTIVITY_FORWARD_RESULT。 As tutorial says: 正如教程所说:

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.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM