Syed Sadman
Syed Sadman

Reputation: 117

Fix ProgressDialog showing up after download is complete

I have a ListView that is populated with articles. When the user long clicks on an article, I want to download its summary and display it to the user. I have a function in AsyncTask (NOT doInBackground...) that takes care of the download. During the downloading process, I want to display a ProgressDialog. Since the download is on the main thread, there is a delay when I long click an article and the ProgressDialog shows AFTER the download is complete. How can I get it to show DURING the download?

I have tried these methods but I don't really understand it since I am a beginner in Android Development. The app freezes while its downloading and shows the Dialog after it's done.

ProgressDialog shows up after thread is done display progressdialog while listview is loading

HomeActivity

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);

    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);


    trendingList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

            try {
            ...
            } catch (Exception e) {

                String link = homeLinks.get(position);
            //TASK TO DOWNLOAD SUMMARIES FROM ASYNCTASK
                task.downloadSummary(link);
                adapter.notifyDataSetChanged();

            }
            return true;
        }
    });

Background Class

public class  BackgroundTask extends AsyncTask<String, Void, String> {

ProgressDialog progress = new ProgressDialog(HomeActivity.this);

 //SHOW DIALOG AND THEN DOWNLOAD SUMMARIES
    public void downloadSummary(String address){

        progress.setMessage("Downloaded summary ");
        progress.show();
        ...

Upvotes: 1

Views: 82

Answers (1)

MarcosApps
MarcosApps

Reputation: 474

When initializing an AsyncTask you must allocate the 3 dependent methods of this thread, onPreExecute that will allocate your ProgressDialog, the InBackground that put the job you want to perform and the onPostExecute that reotrna the result of your work and you close your progressDialog

It had stayed like this

public class  BackgroundTask extends AsyncTask<String, Void, String> {

ProgressDialog progress = new ProgressDialog(HomeActivity.this);


    @Override
    protected void onPreExecute() {
    super.onPreExecute();
    progress.setMessage("Downloaded summary ");
    progress.show();
    }

    @Override
        protected void doInBackground(String... strings) {
    //Ação do Trabalho
    }

    @Override
        protected void onPostExecute(String result) {
        progress.dismiss();
        Log.e("Work", "Result "+result);
    }
}

it is mandatory the google background method does not like when ignoring this use being an AsysnTask will have to use it. If on the contrary if it is a fast job use a thread invoked with run

Upvotes: 1

Related Questions