Devos50
Devos50

Reputation: 2035

Progress Dialog not showing up in AsyncTask

I have an Android application with an AsyncTask which is responsible for downloading a file from the internet. This AsyncTask is executed when clicking on an item in a Listview. So I have a custom adapter and in the OnItemClickListener of the Listview, I start the download and execute the AsyncTask.

Now, my adapter contains the following code to start the AsyncTask named FileDownloader:

@Override
public void onClick(View view) {
     try {
         FileDownloader fd = new FileDownloader(activity);
         // some irrelevant code here
         String filepath = fd.execute("http://myurl.com/img.png", PDFFileName, GameHistoryAdapter.this.gameInfo.toString()).get();
     }
     catch(Exception e) { e.printStackTrace(); }
}

Activity is a private field that is passed to the adapter in the constructor of the adapter:

public GameHistoryAdapter(Activity a, int selectedIndex) {
    this.activity = a;
}

The FileDownloader class contains an OnPreExecute method where I want to show the progress dialog on the activity:

@Override
protected void onPreExecute() {
    super.onPreExecute();

    dialog = new ProgressDialog(activity);
    dialog.setMessage("Downloading...");
    dialog.setIndeterminate(true);
    dialog.setCancelable(true);
    dialog.show();
}

But whatever I try, the dialog does not appear. When I create an alert dialog in the OnPostExecute method of the AsyncTask, the dialog will show.

@Override
protected void onPostExecute(String res)
{
    super.onPostExecute(res);
    dialog.hide();

    new AlertDialog.Builder(activity)
            .setTitle(activity.getString(R.string.save_pdf_title_text))
            .setMessage(activity.getString(R.string.save_pdf_text) + PDFFileName)
            .setPositiveButton(activity.getString(R.string.close_text), null)
            .setIcon(android.R.drawable.ic_dialog_info)
            .show();
}

Does anyone know why the dialog is not appearing on my activity?

Upvotes: 1

Views: 1903

Answers (1)

Squonk
Squonk

Reputation: 48871

Does anyone know why the dialog is not appearing on my activity?

Yes, the following line of code...

String filepath = fd.execute("http://myurl.com/img.png", PDFFileName, GameHistoryAdapter.this.gameInfo.toString()).get();

Don't EVER use the get() method of AsyncTask. It will block the main / UI thread and makes the whole point of an AsyncTask redundant. In other words get() turns it into a synchronous process instead of an asynchronous one.

The fact you can show a dialog in onPostExecute(...) is simply because it will be called after the blocking call to get() has returned. This means the main / UI thread will no longer be frozen (blocked) and UI updates can be made once again.

Remove get() from your call to execute(...) and instead just use...

fd.execute("http://myurl.com/img.png", PDFFileName, GameHistoryAdapter.this.gameInfo.toString());

...then in your onPostExecute(...) method set you filepath variable to what it should be.

I don't know who added the get() method to AsyncTask but if I ever find them I'll have some serious words to say. It has little or no use and causes a lot of people a lot of confusion.

Upvotes: 2

Related Questions