The question is not very clear but I will try to help you
As the others said it would be best for you to use Viewmodel, livedata and room. In the beginning it is a little difficult but later it is much easier and faster to work...
If you do not want to use the room, you may use the viewmodel to inform the fragments in the activity about changes
example:
Create ViewModel class like this
public class MyItemsViewModel extends AndroidViewModel {
private final MutableLiveData<Boolean> refresh;
public MyItemsViewModel(@NonNull Application application) {
super(application);
this.refresh = new MutableLiveData<>();
}
public void setRefresh(Boolean ref) {
this.refresh.setValue(ref);;
}
public MutableLiveData<Boolean> getRefresh() {
return this.refresh;
}
public static class Factory extends ViewModelProvider.NewInstanceFactory {
private final Application application;
public Factory(@NonNull Application application) {
this.application = application;
}
@NonNull
@Override
public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
//noinspection unchecked
return (T) new MyItemsViewModel(this.application);
}
}
}
In Activity where is your FR_MyItems Fragment, (probably your main activity) copy/paste this code (only code below TODO in comment)
@Override
protected void onCreate(Bundle savedInstanceState) {
//TODO copy this in onCreate method
MyItemsViewModel.Factory factory = new MyItemsViewModel.Factory(getApplication());
viewModel = new ViewModelProvider(this, factory).get(MyItemsViewModel.class);
subscribe();
}
and copy/paste complete this method
private void subscribe() {
viewModel.getRefresh().observe(this, refreshed -> {
if(refreshed){
Toast.makeText(this, "Refreshed", Toast.LENGTH_SHORT).show();
}
});
}
- In FR_MyItems Frgament
copy this (compete function)
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if(getActivity() == null) return;
MyItemsViewModel.Factory factory = new MyItemsViewModel.Factory(requireActivity().getApplication());
viewModel = new ViewModelProvider(requireActivity(), factory).get(MyItemsViewModel.class);
subscribe();
}
private void subscribe() {
viewModel.getRefresh().observe(getViewLifecycleOwner(), refreshed -> {
if(refreshed){
//TODO load data from sqlite database and update your MyItemsAdapter with new data list
Toast.makeText(getContext(), "Refreshed Fragment", Toast.LENGTH_SHORT).show();
viewModel.setRefresh(false);
}
});
}
- In your fragment for creating new item (if exist)
copy this
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if(getActivity() == null) return;
MyItemsViewModel.Factory factory = new MyItemsViewModel.Factory(requireActivity().getApplication());
viewModel = new ViewModelProvider(requireActivity(), factory).get(MyItemsViewModel.class);
}
And finaly when you insert new item do it
copy this in method after inserting
viewModel.setRefresh(true);
it will reload adata and refresh your list in FR_MyItems
Also if you don't want insert item in database you can create List myItemsList in MyItemsViewModel create get,set,add methods and observe it from fragments
Add this in class MyItemsViewModel
private List<MyItemsItem>> items
private final MutableLiveData<List<MyItemsItem>> myItemsList;
public MyItemsViewModel(@NonNull Application application) {
super(application);
items= new ArrayList();
this.refresh = new MutableLiveData<>();
this.myItemsList = new MutableLiveData<>();
}
public addItem(MyItemsItem item){
items.add(item);
myItemsList.setValue(items);
}
//TODO create set, get and add methods for list
and observe it in your activity or fragments
viewModel.getMyItemsList().observe(getViewLifecycleOwner(), list -> {
//TODO update adapter with list
});
Also you can use it on other way if you want use notifyItemChanged and don't want to swap data in adapter every time
This is possible in the activity and its fragments.
If you want to refresh the lists between activities you will have to use room or some other way to do so...
EDITED 2
This image is only for your case and it will be deleted soon
In other cases the data is managed (CRUD) through the ViewModel and repository or repositories not in View-UI