NoHistory and the result of children's activity

I have the following case:

Activity AActivity B (set of invalid) → Activity C

I need to get the result from C returned to A when C finishes. Is it possible?

The only way to achieve this is to save the story for B and forward the result from C through it. Thus, although if B starts and the program recovers from the background, B will be displayed. I would prefer that B is not contained in the stack.

+3
source share
2 answers

As Kamen has already confirmed, FLAG_ACTIVITY_FORWARD_RESULT should be used in this case. As the textbook 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.

+2

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();
+3

All Articles