Reputation: 927
i have following Scenario:
Im showing a ProgressDialog in the UI-Thread and on the same time doing a Networkdiscovery in a Backgroundthread. This works just fine. The Dialog has a Stop-Button, so the User can decide if he wants to stop the discovery.
Now here is my Problem: To stop the discovery some heavy workload-process is also done in a Backgroundthread so the Progresswheel in the UI-Thread does not hang up. In theory the Progresswheel should continue spinning until the Backgroundthread has finished its work. But the setButton(...)
-Method automatically dismisses the ProgressDialog, once it reaches the Methodend and by that time the Thread has not finished its work yet. Currently I have no chance to dismiss the Dialog manually through a Messagehandler from the Backgroundthread, because it got dismissed by the OS automatically.
This is a portion of my code:
//Initialize ProgressDialog
ProgressDialog discoveryProgressDialog;
discoveryProgressDialog = new ProgressDialog(context);
discoveryProgressDialog.setTitle("Discover Network");
discoveryProgressDialog.setCancelable(true);
discoveryProgressDialog.setIndeterminate(false); //setting this to true does not solve the Problem
//add Buttonlistener to Progressdialog
discoveryProgressDialog.setButton(DialogInterface.BUTTON_NEUTRAL, "Stop", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
stopDiscoveryTask(); //starts the Backgroundthread
//something here to wait for Thread-Finishing and not freezing
//the UI-Thread or something to prevent dismissing the
//Dialog automatically
}//<--- End will be reached directly after Backgroundthread has started which triggers automatically the dismiss of the Progress-Dialog
});
Upvotes: 3
Views: 1363
Reputation: 41
I found a so so good solution to fix this problem:
1. Derive your class from ProgressDialog.
2. Override dismiss() method, and do nothing there to prevent it from dismissing automatically.
3. In order to dismiss it manually, add a new method.
4. After the background thread ends, call this new method.
public class MyProgressDialog extends ProgressDialog {
@Override
public void dismiss() {
// do nothing
}
public void dismissManually() {
super.dismiss();
}
}
Upvotes: 4
Reputation: 11946
One way might be to have your own cusom dialog view, instead of a ProgressDialog, where you can control when it disappears.
Upvotes: 0
Reputation: 8533
Use a Service
to control the spawning of new background threads. Then use either a ResultReceiver
or Messenger
to pass back UI updates such as progress status (ie start, update, finish);
You can pass the ResultReceiver
or Messenger
as a Parcelable
(no work involved) in the Intent
used to start the Service
.
Upvotes: 0