Caching in Android: Improving Performance and User Experience
Making Android apps faster by storing and reusing frequently accessed data.
Caching is used in Android to improve performance, reduce network usage, and enhance user experience by storing frequently accessed data in memory or on disk.
Faster data retrieval leads to an enhanced user experience.
Caching can be done at two places:
- In Memory 
- On Disk 
Both help with faster data retrieval, hence enhancing the user experience.
Example of caching in Android:
Image Loading Libraries  
Image Loading Libraries like Glide cache images in memory and on disk. It uses the LRU (Least Recently Used) cache internally.
When we provide the URL to the libraries, they do the following:
- They check whether the image with that URL key is available in the memory cache. 
- If the bitmap is present in the memory cache, they just show it by taking it from the memory cache. 
- If not present in the memory cache, they check in the disk cache. 
- If present in the disk cache, they load the bitmap from the disk, also put it in the memory cache, and load the bitmap into the view. 
- If not present in the disk cache, they download the image from the network, put it in the disk cache, also put it in the memory cache, and load the bitmap into the view. 
This way they make the image loading fast as showing directly from the cache is always faster.
OkHttp
OkHttp uses disk cache to store the network response if enabled correctly.
When caching is enabled, OkHttp automatically stores network responses on disk. On subsequent requests, if the cached data is still valid based on the HTTP cache headers, OkHttp returns the cached response instead of hitting the network again, saving bandwidth and improving speed.
This is how caching is used in Android to improve performance, reduce network usage, and enhance user experience by storing frequently accessed data in memory or on disk.
Prepare for your Android Interview: Android Interview Questions and Answers
Thanks
Amit Shekhar
Founder, Outcome School


