views:

47

answers:

2

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...?

+1  A: 

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.

alex
Thanks. This put me onto the right track.
exhuma
+1  A: 

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.

hara