0

Actually I am getting a response using liveData+retrofit from API everything working fine in the first try ..if call the same API again its showing previous response first and then new response override the previous one. I debug the code and it happens because of Live data onChanged method called as I enter to the fragment so showing previous pojo result which is got at the first attempt. For some time it's showing the last result and once API response came then actual response load to recycler view.

viewModel.getMutuableData().observe(getActivity(), new Observer<Model>() {
    @Override
    public void onChanged(@Nullable Model model) {
        list.clear();
        if (model.getResult()) {
            list.addAll(model.getCategoryList());
        }
        adapter.notifyDataSetChanged();
    }
});
Natig Babayev
  • 3,128
  • 15
  • 23
Bunny
  • 1,044
  • 12
  • 23
  • 2
    If your viewmodel is created using `Activity` context, the `LiveData` might retain the previous value in that activity instance. – Hussain Sep 11 '19 at 05:30
  • Can you add code for invoking network request which creates livedata? – Natig Babayev Sep 11 '19 at 05:50
  • Please refer this [Answer](https://stackoverflow.com/questions/51757164/how-to-create-livedata-which-emits-a-single-event-and-notifies-only-last-subscri) written in kotlin – Dipak Nai Sep 11 '19 at 06:29

0 Answers0