android AsyncTask does not dismiss Progressbar

In my app i need to make call for AsyncTask in for loop like this

 protected void runSearch(String query) {


            for (int i = 0; i < MAX_RESULT_COUNT; i += PAGE_SIZE) {
                try {
                    String s = SEARCH_ENDPOINT + "&q="
                            + URLEncoder.encode(query, "UTF-8") + "&start=" + i;
                    // Toast.makeText(this, "qUERY>>  " + s + "",
                    //
                    // Toast.LENGTH_SHORT).show();
                    System.out.println("THISI SSS>>>>> ss" + s + "");

                    String se = SEARCH_ENDPOINT + "&q="
                            + URLEncoder.encode(query, "UTF-8") + "&start=" + i;


                    Get_Images get_images = new Get_Images();
                    get_images.execute(se);


                } catch (UnsupportedEncodingException e) {
                    // TODO: handle error
                }
            }
        }

class Get_Images

public class Get_Images extends AsyncTask<String, Void, Void> {
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                pDialog = new ProgressDialog(getActivity());
                pDialog.setMessage("Please wait...");
                pDialog.setCancelable(true);
                pDialog.show();
            }


            @Override
            protected Void doInBackground(String... params) {

                ServiceHandler sh = new ServiceHandler();

                // Making a request to url and getting response

                return null;

            }

            @Override
            protected void onPostExecute(Void aVoid) {
                super.onPostExecute(aVoid);
                 if (pDialog.isShowing())
                 {
                 pDialog.dismiss();
                 }
                if (ThumbImage != null) {

                    //Adpter fill
                }
            }

    }

The Problem is it does not dismiss Progress dialog. if it has created 3 dialog then does not dismiss any of them.

Pleas help Thanks,

Answers


I think by calling AsyncTask in for loop, it might start another task while the previous one still in doInbackground() that will result in multiple progress running because of this:

pDialog = new ProgressDialog(getActivity());

You will have a reference on the last started progress, so calling dismiss will only hide last one, while the other X progress(es) will remain active.

So, either:

  1. Start the progress one time, before starting the for loop and stop it at onPostExecute() of last asyncTask

Or

  1. Move the for loop inside doInBackground() as @StackOverflowUser stated

Need Your Help

Possibility to ping test dual sim 3G networks simultaneously?

android ping 3g android-networking dual-sim

I have built an app to schedule ping service continuously for the connected network using parameters like timeout and timeintervals between pings from the user. The app works fine. I had a doubt ab...

Successful FPGA application for HPC, e.g. on a cluster with InfiniBand backbone?

cluster-computing fpga hpc

Assuming there is a task (e.g. an image processing method with a lot math) which is reasonable to be implemented on FPGA in sense of answer https://stackoverflow.com/a/8695228/544463