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...?
Dialog gets cached by
Activity, that's the expected behavior.
onPrepareDialog(..)to reset it's state
removeDialog(..)before showing it again
I usually go #1 for frequently used
Dialogs and #3 for the rare ones.