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.
Asked
Active
Viewed 236 times
0
-
did you call API in onResume() ? – Sniffer Jul 16 '21 at 04:39
1 Answers
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.
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