0

I have a fragment A which fetches fresh data from network and shows a list using. Now I switch to other fragment B and comes back to the fragment A, it fetches the data again from network. How can I avoid the network call? I am using viewmodel and livedata to fetch the data.

takharsh
  • 2,258
  • 1
  • 21
  • 26

1 Answers1

1
  1. You can use retrofit and add a cache mechanism. Can Retrofit with OKHttp use cache data when offline

    https://bapspatil.medium.com/caching-with-retrofit-store-responses-offline-71439ed32fda

    These will help you.

  2. Use shared ViewModel and Repository pattern, use single livedata object, which stores last hit response... check liveData has API response, dont fetch else fetch new response.

I recommend first solution

Manmohan
  • 410
  • 4
  • 13