Reputation: 43
I would like to pass arguments from an activity B to A where B has been launched by A. Is this possible to do so ? Thanks
Upvotes: 4
Views: 2375
Reputation: 23873
To expand a bit on davec's answer:
If you need more data than just RESULT_OK, then you will have to use putExtra() in B and getExtras() in A. You can send primitive data types, e.g for String:
In B:
String str1 = "Some Result";
Intent data = new Intent();
data.putExtra("myStringData", str1);
setResult(RESULT_OK, data);
Then to pick it up in A:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (data != null) {
Bundle b = data.getExtras();
String str = b.getString("myStringData");
}
}
}
.
Upvotes: 6
Reputation: 5251
Look at startActivityForResult (to be called from A), setResult (to be called from B), and onActivityResult (A's callback that gets called after B exits).
Upvotes: 2
Reputation: 10908
Yes, if when you launch Activity
B from A, you start it using startActivityForResult
then you can set a result in Activity
B then read the value in A.
In A you would need to override onActivityResult
to get the result value.
In Activity
B:
// do stuff
setResult(RESULT_OK);
finish();
Then in A:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
//check result
}
Upvotes: 10