Call ActivityB from FragmentA (which is part of ActivityA) as startActivityForResult() instead of startActivity() call.
Using this, you would be able to pass back result from Activity B to Fragment A.
Fragment A (Part of ActivityA) :
// Calling Activity B
Intent intent = new Intent(this, ActivityB.class);
intent.putExtras(b);
startActivityForResult(intent, ANY_ID);
// Overriding callback for result
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == ANY_ID && resultCode == Activity.RESULT_OK) {
// Your logic of receiving data from Activity B
}
}
Important Point : The Fragment A is the one making the startActivityForResult() call, but it is part of Activity A so Activity A gets the first shot at handling the result. It has to call super.onActivityResult() so that the callback can come to Fragment A
In Activity A :
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// This is required, in order to get onActivityResult callback in Fragment A
}
Setting result back from Activity B :
Intent resultIntent = new Intent();
// You can pass any data via this intent's bundle by putting as key-value pair
resultIntent.putExtra("Key", Value);
setResult(Activity.RESULT_OK, resultIntent);
finish();
Reference :
- https://stackoverflow.com/a/22554156/1994950
- https://stackoverflow.com/a/6147919/1994950
- Start Activity for result