Reputation: 53657
I have created a progress dialog by
ProgressDialog progressDialog = null; // create instance variable of ProgressDialog
int dialogID = 1;
//to create progress dialog
protected Dialog onCreateDialog(int id) {
progressDialog = new ProgressDialog(context);
progressDialog.setMessage(message);
progressDialog.setIcon(android.R.id.icon);
return progressDialog;
}
// to show progressdialog
showDialog(dialogID);
To remove the dialog I am able to use any of the following three approaches
approach-1
if(progressDialog != null){
progressDialog.dismiss();
}
approach-2
if(progressDialog != null){
progressDialog.cancel();
}
approach-3
removeDialog(dialogID);
I found second approach is more effective than first approach. and if I have to use with more than one progressdialog it is easier to use approach-3. But what is the best way to destroy a progressdialog and How?
Upvotes: 5
Views: 5412
Reputation: 59168
cancel();
is better than dismiss()
because according to docs:
Cancel the dialog. This is essentially the same as calling
dismiss()
, but it will also call yourDialogInterface.OnCancelListener
(if registered).
removeDialog()
is deprecated so that would be the worst way.
Upvotes: 7
Reputation: 1
Depends on what you want to achive. If you want to create the Processdialog everytime with a new message, you need to use the onPrepareDialog()
Method or you use removeDialog()
Method on a shown Dialog. Because the next time you use yourDialog.show()
the onCreate()
Method is called again, if you just dismiss your Dialog its not called the next time you show it. onPrepareDialog()
is called everytime.
http://developer.android.com/guide/topics/ui/dialogs.html
Upvotes: 0
Reputation: 128428
I think below way is the best to dismiss the progress bar from screen:
if(progressDialog.isShowing())
{
progressDialog.dismiss();
}
Upvotes: 1