Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8141787
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T12:37:42+00:00 2026-06-06T12:37:42+00:00

I have an Activity with a button that starts a long running task. I

  • 0

I have an Activity with a button that starts a long running task. I perform the task using an AsyncTask and display a determinate ProgressBar that is updated as the task progresses. Everything works fine the first time the task is run, but on subsequent runs the dialog appears, but is already at 100% and 100/100. I’m overriding both onCreateDialog and onPrepareDialog in my Activity. I presumed that using setProgress(0) would reset the dialog, but this isn’t working. I use showDialog in the AsyncTask’s onPreExecute method and dismissDialog in the AsyncTask’s onPostExecute. The only way I’ve been able to get the dialog to reset is by using removeDialog in onPostExecute, but I would rather not recreate the dialog as I’m going to be reusing several times. The relevant code is below. This is on API level 10.

@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
        case PROGRESS_DIALOG:
            progressDialog = new ProgressDialog(ProtoBufSerializationTestActivity.this);
            progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            progressDialog.setCancelable(true);
            progressDialog.setMax(100);
            progressDialog.setMessage("Testing...");
            return progressDialog;
        default:
            return null;
    }
}

@Override
protected void onPrepareDialog(int id, Dialog dialog) {
    super.onPrepareDialog(id, dialog);
    switch (id) {
        case PROGRESS_DIALOG:
            ProgressDialog tmp = (ProgressDialog)dialog;
            tmp.setProgress(0);
            tmp.setSecondaryProgress(0);
            return;
        default:
            Log.e("test", "Unknown dialog requested");
    }
}

private class SerializationTestTask extends AsyncTask<Void, Integer, Long> {

    @Override
    protected void onPreExecute() {
        showDialog(PROGRESS_DIALOG);
    }

    @Override
    protected Long doInBackground(Void... params) {
        long accumulator = 0;
        int totalSize = 0;
        for (int i = 0; i < 10000; i++) {
            final long start = System.nanoTime();
            //code to be timed
            final long end = System.nanoTime();
            accumulator += end - start;
            if (i % 100 == 0) {
                publishProgress(i);
            }
        }
        return accumulator;
    }

    @Override
    protected void onPostExecute(Long aLong) {
        dismissDialog(PROGRESS_DIALOG);
        results.setText(String.format("Serialized Message 10000 times.\nAverage time to serialize %.3fms", (double) aLong / 1e10));
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        progressDialog.incrementProgressBy(1);
    }

Edit After taking @Arhimed’s suggestion I ended up with the following code. It is much simpler. I could have used DialogFragment, but I don’t think it is necessary in this case.

private class SerializationTestTask extends AsyncTask<Void, Integer, Long> {
    private ProgressDialog dialog;

    @Override
    protected void onPreExecute() {
        dialog = ProgressDialog.show(ProtoBufSerializationTestActivity.this, "Serialization Test", "Testing...", false);
    }

    @Override
    protected Long doInBackground(Void... params) {
        long accumulator = 0;
        int totalSize = 0;
        for (int i = 0; i < 10000; i++) {
            final long start = System.nanoTime();
            //code to be timed
            final long end = System.nanoTime();
            accumulator += end - start;
            if (i % 100 == 0) {
                publishProgress(i);
            }
        }
        return accumulator;
    }

    @Override
    protected void onPostExecute(Long aLong) {
        dialog.dismiss();
        results.setText(String.format("Serialized Message 10000 times.\nAverage time to serialize %.3fms", (double) aLong / 1e10));
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        dialog.incrementProgressBy(1);
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T12:37:44+00:00Added an answer on June 6, 2026 at 12:37 pm

    It is impossible to use ProgressDialog in that way. Your AsyncTask should create a new instance each time.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an activity that starts a long-running service which in turn adds an
I have 4 buttons that when click starts a new activity class. This works
I have activity A that starts activity B which starts activity C: A ->
In my activity class i want to perform a series of long calculations that
I have an activity that starts on demand of the user. The user can
Suppose I have an activity A that starts activity B and kills itself. Now
I have a Notification which starts an Activity. After a long press on home
I have an activity that starts after a certain time. Once this activity starts,
I have an activity with a single TextView (tv1) and a button that, when
I have a simple application that starts with a splash screen activity that plays

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.