Why my progressDialog never ends?

309 views Asked by At

I have a MainActivity class in my Android project. Inside of it, I have an AsyncTask to retrieve all the information about my Cars with a GET method of my API REST and I want to add them to an ArrayList that I have in my MainActivity class. Because of that, I wanted to add a ProgressDialog that finish at the same time that my AsyncTask finish.

I have declared my ProgressDialog on the top of my MainActivity:

private ProgressDialog progressDialog;

And also, my ArrayList of cars it's defined on the top of my MainActivity:

ArrayList<Car> cars = new ArrayList<Car>(); //This is the ArrayList that I have on my MainActivity class

Then, I run my AsyncTask and initialize my ProgressDialog in the onCreate method of my MainActivity class:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        progressDialog = new ProgressDialog(MainActivity.this);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressDialog.setMessage("Loading...");
        progressDialog.setCancelable(true);
        progressDialog.setMax(100);

        new chargeCars().execute();
    }

And here it's my AsyncTask:

class chargeCars extends AsyncTask<Void, Integer, Boolean> {

    protected void onPreExecute(){
        progressDialog.setProgress(0);
        progressDialog.show();
    }

    protected Boolean doInBackground(Void... params) {

        //Here all the code to get my information from an HTTP Response, the data it's retrieving OK.

           //Here there is a for loop to add all the elements to my ArrayList cars.
            {
                cars.add(new Car(idCar, model));

                publishProgress(10);

                if(isCancelled())
                    break;
            }

        publishProgress(100);
        return true;
    }

    protected void onProgressUpdate(Integer... values){
        int progress = values[0].intValue();

        progressDialog.setProgress(progress);
    }

    protected void onPostExecute(boolean c){

        if(c)
        {
            Toast.makeText(MainActivity.this, "Finished task!",
                    Toast.LENGTH_SHORT).show();
        }

        if(progressDialog != null && progressDialog.isShowing()){
            progressDialog.dismiss();
        }
    }

    protected void onCancelled() {
        Toast.makeText(MainActivity.this, "Canceled task",
                Toast.LENGTH_SHORT).show();
    }
}

The problem it's that the ProgressDialog never ends, it's always on the screen. Why it is always displaying? Is my AsyncTask always running? Never ends?

Note: The information of my HTTP response it's coming well from my API REST because I put some logs after I retrieve the information and it is correct and apparently it is stored in the ArrayList (I put some logs too after add the information to my ArrayList and it gives me the information stored) but when I put cars.size() after executing my AsyncTask on the onCreate() method it gives to me that the size it's equals to 0. It's the reason that I put my ProgressDialog inside of my AsyncTask, because I think it maybe didn't finished yet and then I'm secure that the AsyncTask has finished. Could it be the solution? Have I to make another change?

Please bring me light because I'm totally block here.

EDIT: I edited my code.

EIDT 2: With the code updated the ProgressDialog only disappears when I touch the screen of my mobile phone. In other cases it doesn't disappear.

Thanks in advance!

3

There are 3 answers

1
Jayesh Elamgodil On

In your onPostExecute add,

if(progressDialog != null && progressDialog.isShowing()){
        progressDialog.dismiss();
    }
3
Jorgesys On

use the dismiss() method!

  @override
   protected void onPostExecute(boolean c){

            if(c)
            {
                Toast.makeText(MainActivity.this, "Finished task!",
                        Toast.LENGTH_SHORT).show();
            }
           if(progressDialog != null && progressDialog.isShowing()){
                 progressDialog.dismiss();
           }
        }

dismiss ()

Added in API level 1 Dismiss this dialog, removing it from the screen. This method can be invoked safely from any thread. Note that you should not override this method to do cleanup when the dialog is dismissed, instead implement that in onStop().

1
Joseph Roque On

The problem is that your methods onPause and onDestroy in your AsyncTask do not override anything in the class and are therefore never called. If you need something done when the AsyncTask is finished, you put it in the method onPostExecute (which runs on the UI thread). You should remove these two methods (since they're useless) and set your onPostExecute method as follows:

protected void onPostExecute(boolean c){
    if(c) {
        Toast.makeText(MainActivity.this, "Finished task!",
                Toast.LENGTH_SHORT).show();
    }

    if(progressDialog != null && progressDialog.isShowing()){
        progressDialog.dismiss();
    }
}

See the Android docs for methods available in AsyncTask