Использование startActivityForResult
.
private static int final ADD_CONTACT_CODE = 1;
// MainActivity
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == ADD_CONTACT_CODE && resultCode == RESULT_OK) {
// data is the contactIntent you set in AddNewContactActivity
// update your fragment here with that data
}
}
// addContactButton in MainActivity
onClick {
startActivityForResult(addNewContactIntent, ADD_CONTACT_CODE);
}
// AddNewContactActivity
// call this when you're finish adding contact
void finishAddContact() {
Intent contactIntent = Intent();
// add your added contact here
contactIntent.putExtra("added contact", addedContact)
setResult(RESULT_OK, contactIntent);
finishActivity(ADD_CONTACT_CODE); // this will close this activity returning to the MainActivity and calling its onActivityResult
}