How do you clear a specific progress bar for reuse?

I have an action with a button that launches a lengthy task. I am executing a task using AsyncTask and showing a specific ProgressBar, which is updated as the task progresses. Everything works fine at the first start of the task, but on subsequent launches a dialog box appears, but it is already 100% and 100/100. I redefine both onCreateDialog and onPrepareDialog in my activity. I assumed that using setProgress (0) would reset the dialog box, but this does not work. I use showDialog in the AsyncTask onPreExecute method and disable Dialog in AsyncTask onPostExecute. The only way I was able to get the reset dialog is to use removeDialog in onPostExecute, but I would prefer not to recreate the dialog since I am going to reuse it several times. The corresponding code is given below. This is at 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); } 

Change After accepting the @Arhimed offer, I got the following code. It is much simpler. I could use 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); } } 
+4
source share
3 answers

Therefore, it is not possible to use a ProgressDialog . Your AsyncTask should create a new instance each time.

+1
source

Actually, Archimedes is right, and you can’t do this, but you can make the progress indicator go to zero with a dirty way. For me, the client wanted the progress indicator to be zero, regardless of the fact that if the user left this activity, but in my case the progress was still there, and only it was overwritten from the very beginning. I had custom progress, and setProgress didn't help me at all. So I did this to call this method before actually setting up REAL drawable setProgressDrawable(null) , this is a dirty cleanup, but it does what you want and then just calls the same method its own progressdrawable, so it seems like it was updated. This is not so. You can achieve this, unfortunately.

+2
source

In fact, setProgress (0) only updates the display if progress is running. This way you can force update by calling onStart before setProgress, for example:

 @Override protected void onPrepareDialog(int id, Dialog dialog) { super.onPrepareDialog(id, dialog); ProgressDialog progress = (ProgressDialog)dialog; progress.onStart(); progress.setProgress(0); } 
0
source

All Articles