I want to add file path to Db and when a file already exists in DB show a Toast message. In ViewModel class:
public void addFile(SharedFile file) {
DefaultExecutorSupplier.getInstance().forBackgroundTasks()
.execute(() -> {
long result = fileRepository.insert(file);
insertResult.postValue(result);
}
);
}
public MutableLiveData<Long> getInsertResult() {
return insertResult;
}
and in the Fragment onViewCreated
:
viewModel.getInsertResult().observe(getViewLifecycleOwner(), aLong -> {
if (aLong == -1) {
Toast.makeText(getContext(), getString(R.string.already_exist_file), Toast.LENGTH_LONG).show();
}
});
It works and when I add a repetitive file it Toasts the message, but the problem is when I open another fragment and back to the current fragment it again Toasts message.