First Activity in my App has three tabs first tab include Recycle-view that contains list of images and these images comes from webservice when click on tab number two or three and then click on tab number one images load from WebService Again i want to saveInstance for fragment
Asked
Active
Viewed 444 times
1 Answers
0
Firstly, in your recycler view's adapter use Picasso library to display images. It has image cache that works out of the box. When you download image for the first time it will be cached on your device. When you try to download image with the same URL again (when you returned from the third tab) Picasso will show image from the cache and won't download it again.
Secondly, you can save your image list in the fragment. In your fragment override
String BUNDLE_IMAGES = "imgs";
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
ourState.putStringArrayList(BUNDLE_IMAGES , getImageArray())
}
private List<String> getImageArray(){
//your implementation to get image array
}
To restore
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if (savedInstanceState != null) {
List<String> images =savedInstanceState.getStringArrayList(BUNDLE_IMAGES);
}
}
More info on saving/restore fragments state : Once for all, how to correctly save instance state of Fragments in back stack?
UPDATE: Use custom image cache:
private LruCache<String, Bitmap> mMemoryCache;
@Override
protected void onCreate(Bundle savedInstanceState) {
...
// Get max available VM memory, exceeding this amount will throw an
// OutOfMemory exception. Stored in kilobytes as LruCache takes an
// int in its constructor.
final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
// Use 1/8th of the available memory for this memory cache.
final int cacheSize = maxMemory / 8;
mMemoryCache = new LruCache<String, Bitmap>(cacheSize) {
@Override
protected int sizeOf(String key, Bitmap bitmap) {
// The cache size will be measured in kilobytes rather than
// number of items.
return bitmap.getByteCount() / 1024;
}
};
...
}
public void addBitmapToMemoryCache(String key, Bitmap bitmap) {
if (getBitmapFromMemCache(key) == null) {
mMemoryCache.put(key, bitmap);
}
}
public Bitmap getBitmapFromMemCache(String key) {
return mMemoryCache.get(key);
}

Community
- 1
- 1

HellCat2405
- 752
- 7
- 16
-
firstly i 'm working on (AWS)Amazon Webservice and download image not use URl – yousef Nov 03 '15 at 15:29
-
Can you help me to make cache images after downloading without Using Picasso @HellCat2405 – yousef Nov 03 '15 at 17:05