The difference between removeDialog(), dismissDialog(), and dismiss().
What is the difference between removeDialog()
and dismiss() and dismissDialog()
? Because I can use them together without any problem.
Is it important to implement DialogInterface.OnClickListener
or AlertDialog.OnClickListener
?
I searched a lot but couldn’t find anything useful.
Edit:
I’m developing for Android 2.3.
Sample code:
public final class OptionsPreference extends PreferenceActivity implements DialogInterface.OnClickListener
{
private AlertDialog noInternetDialog = null;
//...
@Override
protected void onPause()
{
if (this.noInternetDialog != null)
{
Log.d(LOG_TAG, "Destroying noInternetDialog...");
this.noInternetDialog.dismiss(); X?
removeDialog(DIALOG_NOINTERNET); X?
dismissDialog(DIALOG_NOINTERNET); X?
this.noInternetDialog = null;
}
super.onPause();
}
@Override
protected final Dialog onCreateDialog(final int id)
{
switch (id)
{
case DIALOG_NOINTERNET:
{
final AlertDialog.Builder _builder = new AlertDialog.Builder(this).setIcon(android. R.drawable.ic_dialog_info).setMessage(R.string.str_nointernet);
_builder.setCancelable(false);
_builder.setPositiveButton(R.string.str_wifisettings, this);
_builder.setNeutralButton(R.string.str_ok, this);
this.noInternetDialog = _builder.create();
if (!isFinishing())
{
this.noInternetDialog.show();
}
return this.noInternetDialog;
}
// ...
}
Solution
dismissDialog(int id):
Closes the dialog box with the specified ID. It only hides the dialog box, but still retains an internal reference to the activity that contains the dialog box so that it can be restored in the future. Deprecated in API 13.
removeDialog(int id):
It also closes the dialog box with the specified ID. Meaning that it hides that particular dialog box, in addition to clearing all references to the activity, so it can’t be restored in the future. Deprecated in API 13.
dismiss():
This method runs on a specific dialog because it is a method of the Dialog class. It also closes the dialog box. You must have a valid dialog box to close it, otherwise you will get an exception.