I've a fragment which is called from the MainActivity. And this Fragment fetches a JSON file of image URL's and displays images on the screen. I've used Fresco library to download these images and then pop as a image list view. The images are automatically cached by the Fresco Library.
I want to remove all cached images when I perform a back press from the Fragment. The images are immediately uncached as the user scrolls down the list and I want to uncache all images when user performs a backPress in the Fragment
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_gallery, container, false);
private List<gallery_adapter> gallery_adapterList = new ArrayList<>();
ListView list = (ListView) view.findViewById(R.id.galleryList);
gallery_adapterList.clear();
if(isNetworkConnected()){
new galleryImages().execute("http://www.myurl.json");
}else
Toast.makeText(context,"No Internet Connection!",Toast.LENGTH_SHORT).show();
return view;
}
public class galleryImages extends AsyncTask<String, String, String> {
HttpURLConnection connection;
BufferedReader reader;
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... params) {
try {
URL url = new URL(params[0]);
connection = (HttpURLConnection) url.openConnection();
InputStream stream = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(stream));
StringBuilder builder = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
}
String str = builder.toString();
return str;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
if(s!=null){
try {
JSONObject parent = new JSONObject(s);
JSONArray images = parent.getJSONArray("images");
if(images!=null){
for(int i=0; i<images.length();i++){
JSONObject child = images.getJSONObject(i);
gallery_adapterList.add(new gallery_adapter(child.getString("url"),child.getString("text")));
}
displayList();
}else
Toast.makeText(context,"error",Toast.LENGTH_SHORT).show();
} catch (JSONException e) {
Toast.makeText(context, "Images not yet added. Try later",Toast.LENGTH_SHORT).show();
}
}else
Toast.makeText(context, "Images not yet added. Try later",Toast.LENGTH_SHORT).show();
}
}
Display list method
private void displayList() {
ArrayAdapter<gallery_adapter> adapter = new galleryAdapterList();
list.setAdapter(adapter);
}
public class galleryAdapterList extends ArrayAdapter<gallery_adapter> {
galleryAdapterList() {
super(context, R.layout.gallery_item, gallery_adapterList);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(context);
convertView = inflater.inflate(R.layout.gallery_item, parent, false);
}
gallery_adapter current = gallery_adapterList.get(position);
TextView mText=(TextView) convertView.findViewById(R.id.galleryText);
mText.setText(current.getText());
uri = Uri.parse(current.getUrl());
//code to remove images from cache
ImagePipeline imagePipeline = Fresco.getImagePipeline();
imagePipeline.evictFromCache(uri);
//code ends here
SimpleDraweeView mImage = (SimpleDraweeView) convertView.findViewById(R.id.galleryImage);
mImage.setImageURI(uri);
return convertView;
}
}