user8447949
user8447949

Reputation: 43

Getting a Result from an Activity

To describe the situation, say that I have 3 activities: A, B, and C and there is a button in activity A which starts activity B, and there is a button in activity C when it is clicked, it should send a result from activity C to activity A

My Question is...Is there a way to pass a result from C to A? If there is a way, what it is?

Note: It would be good if the way you give uses the methods startActivityForResult(...) and onActivityResult(...)

Thank you in advance

Upvotes: 1

Views: 169

Answers (3)

R. Zagórski
R. Zagórski

Reputation: 20258

There's a flag of Intent called FLAG_ACTIVITY_FORWARD_RESULT. Call:

Intent intent = new Intent(this, ActivityB.class);
startActiivtyForResult(intent);

when starting Activity B (by calling startActivityForResult(intent)). When opening C, call:

Intent intent = new Intent(this, ActivityB.class);
intent.setFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
startActiivtyForResult(intent);
finish();

which means: open C, close B. Then, when closing Acitivity C, call:

setResult(123);
finish();

the result from C will go back to A.

Upvotes: 1

josh_gom3z
josh_gom3z

Reputation: 294

You can use Intent to transfer data if you are navigating from Activity C to Activity A. Otherwise, I would suggest using an Interface, and then pass the data as parameter in a callback method present in Activity A. You said you wanted answers like onActivityResult, for that I guess the Activity C should exit or something to invoke onActivityResult in the Activity A

Upvotes: 0

Leontsev Anton
Leontsev Anton

Reputation: 817

If you just using simple types like String objects you can use Bundle and supplementary variables in B,C activities. And transfer it from C->B->A using onActivity result. Or you could use Shared preferences.

Upvotes: 1

Related Questions