Now Google has released Android Architecture Components library, the best solution is to use a shared ViewModel between both Fragments with your Activity as the one that implements LifecycleRegistryOwner.
Doing this, we will avoid refreshing UI calling onResume and we don't need to code any tricky solution.
A solution could be the following one:
public MyActivity extends AppCompatActivity {
...
}
public class FragmentA extends Fragment implements OnClickListener {
...
private SharedViewModel mSharedViewModel;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mSharedViewModel = ViewModelProviders.of(getActivity()).get(SharedViewModel.class);
mSharedViewModel.getData().observe(this, value -> {
updateUIValue(value);
});
}
@Override
public void OnClick(View v) {
if (v == dialogButton) {
showDialog();
}
}
@Override
public void OnClick(View v) {
if (v == dialogButton) {
showDialog();
}
}
public void showDialog() {
String diagName = getResources().getString(R.string.dialog_title);
MyDialog myDialog = MyDialog.newInstance(getFragmentAValue());
myDialog.show(getFragmentManager(), diagName);
}
}
public class MyDialog extends DialogFragment implements OnClickListener {
...
private SharedViewModel mSharedViewModel;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mSharedViewModel = ViewModelProviders.of(getActivity()).get(SharedViewModel.class);
mSharedViewModel.getData().observe(this, value -> {
updateUIValue(value);
});
}
@Override
public void onClick(View view) {
if (view == acceptButton) {
...
mSharedViewModel.setValue(newValue);
}
else if (view == cancelButton) {
dismiss();
}
}
}
public class SharedViewModel extends ViewModel {
final MutableLiveData<Resource<Value>> data = new MutableLiveData<>();
public MutableLiveData<Value> getData() {
return data;
}
public void setValue(Value newValue) {
this.data.setValue(newValue)
}
}