0

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;
    }

}
Avinash_ks
  • 173
  • 3
  • 8

2 Answers2

1

I think what you was it's this however if I may why do you want to clear the cache? Because the next time the user opens the activity he needs to wait for the json response again

alb
  • 347
  • 2
  • 7
  • 24
0

Below are option that you can do so:

1.You can cache backpress event in Fragment and call your function to clear cache images ,Handle Back Press.

2.Track Fragment by it's onAttach(Context) , Mean while you need to write FragmentManager.addOnBackStackChangedListener(); it will be called if you have added your fragment with addtoBackStack .

Community
  • 1
  • 1
Herry
  • 7,037
  • 7
  • 50
  • 80
  • This method will be called in the MainActivity right ? So how will I get the list of URL's in the MainActivity so that they can be Uncached – Avinash_ks Mar 21 '17 at 12:29
  • You may use Application class to fetch list of URL or may be declare interface to communicate with Fragment to Activity – Herry Mar 21 '17 at 12:31
  • Thanks @Herry. Used the Application class to fetch the details and then sent it to the Fragment for displaying images – Avinash_ks Mar 22 '17 at 03:48
  • if my answer help you to resolved you can accept it @Avinash_ks – Herry Mar 22 '17 at 05:21