I have 2 fragments in my app. when the user clicks on a button in the first fragment, the second fragment gets added so that the user can insert some data. and then it gets closed and gives the inserted data back to the first fragment. I've used ViewModels for this communication between fragments.
collectionsEditedViewModel = new ViewModelProvider(getActivity()).get(CollectionsEditedViewModel.class);
collectionsEditedViewModel.isEdited().observe(getViewLifecycleOwner(), new Observer<Bundle>() {
@Override
public void onChanged(Bundle bundle) {
}
});
the communication is working properly. but the point is that how can I define the scope of this communication within the fragments. currently I'm using getActivity()
as ViewmodelStoreOwner
which causes the set data to be redelivered to the first fragment whenever it is reopened. how can I solve this issue?