Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ProgressBar does not reset to "0" when opening the second time

In an Android application I run a task in a separate thread. While the thread is running it updates a progress bar in the main window to let the user know what's going on. The problem is, that if a user starts the thread a second time the progress bar won't be reset. It will just sit on 100% without doing anything.

The whole source is available on github. As an example take the BackupThread and JsonBackup (the main class). Note that "RestoreThread" shows the same behaviour, but I assume that in both cases the problem is identical.

On to a quick walk through:

At around line 300 in JsonBackup.java, I open a new ProgressDialog. As it's a new instance, I assume all is initialised properly. On my current working copy I have added a line setting the progress to "0", but that didn't change anything. In the same block, I create the backup thread and run it.

The next bit of code of interest is the moment when the thread communicates it's progress back to the main application. This happens around line 185 of BackupThread.java.

The message is handled in line 80 of JsonBackup.java which will update the progress bar.

So far for the code walkthrough. One thing that struck me while writing this is line 87 of JsonBackup.java. I think it would be best if the thread sent a different message, explicitly stating that it's done. But I assume this is not the real problem here. I say this because the progress dialog properly dismisses itself once it reaches 100%.

So, if this whole thing is run once, everything works as expected. Now assume, that the user leaves the application, but the OS decides to leave it loaded in memory. Later the user comes back and wants to create a new backup. Then the app won't work as the progress hangs at 100%. If you kill the app, so it's completely unloaded it will work again. So I assume that some object instance is still in memory and has an unclean state when re-run. But for the life of me, I cannot find what's going on. As a tangent I might add: Apart from the Notepad tutorials, this is my first Android app, and bla di blah blah...

What am I missing here...?

like image 738
exhuma Avatar asked May 17 '10 18:05

exhuma


People also ask

How to use progress bar in livecode?

Test the progress bar by switching to run mode and clicking the button. You should see that progress increases with each button click, until the progress bar reaches the end of the progress display. Continued clicks on the button cause the progress bar to reset and progress to start from the beginning.

How does progress bar work in C#?

By using the ProgressBar in your application, you alert the user that the application is performing a lengthy task and that the application is still responding. The Maximum and Minimum properties define the range of values to represent the progress of a task.

How to do progress bar in html?

Use the <progress> tag to create a progress bar in HTML. The HTML <progress> tag specifies a completion progress of a task. It is displayed as a progress bar. The value of progress bar can be manipulated by JavaScript.

How use progress bar in C# Windows application?

To create a ProgressBar control at design-time, you simply drag a ProgressBar control from the Toolbox and drop onto a Form in Visual Studio. After you the drag and drop, a ProgressBar is created on the Form; for example the ProgressBar1 is added to the form and looks as in Figure 1.


2 Answers

The Dialog gets cached by Activity, that's the expected behavior.
Options are:

  1. override onPrepareDialog(..) to reset it's state
  2. call removeDialog(..) before showing it again
  3. manage the dialog yourself, manually calling show(), dismiss()

I usually go #1 for frequently used Dialogs and #3 for the rare ones.

like image 125
yanchenko Avatar answered Sep 22 '22 10:09

yanchenko


i usually use onCreateDialog() plus onPrepareDialog() to manage dialogs.

From dev guide on android developers:

The best way to define the onCreateDialog(int) and onPrepareDialog(int, Dialog) callback methods is with a switch statement that checks the id parameter that's passed into the method. Each case should check for a unique dialog ID and then create and define the respective Dialog.

onCreateDialog() method is called once when the dialog is created for the first time; puts here the code that initialize your dialogs.

onPrepareDialog() method is called each time the dialog is shown.

It Provides an opportunity to prepare a managed dialog before it is being shown. Override this if you need to update a managed dialog based on the state of the application

When the dialog has to be closed you could:

If you are using onCreateDialog(int) to manage the state of your dialogs (as discussed in the previous section), then every time your dialog is dismissed, the state of the Dialog object is retained by the Activity. If you decide that you will no longer need this object or it's important that the state is cleared, then you should call removeDialog(int). This will remove any internal references to the object and if the dialog is showing, it will dismiss it.

like image 26
hara Avatar answered Sep 22 '22 10:09

hara