开发者

noHistory and child activity result

I have the following case:

Act开发者_如何学Civity A -> Activity B (noHistory set) -> Activity C

I need to have a result from C returned to A when C finishes. 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. Thus, though, if B is launched and the program is restored from background, B will be displayed. I would prefer that B is not kept on the stack.


Additional details

noHistory and child activity result

In activity A:

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

In activity A:

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

In activity B:

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

In activity 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. 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜