How can I kill a custom dialog - before it's been created?
Clearly I have a case of what I like to call "chicken or the egg" syndrome. I am trying to find a way to kill an alert dialog which isn't created unt开发者_JS百科il the end of the process.
What I am trying to do: When asked "Do you want to RE-DOWNLOAD this gallery?", I want to KILL the underlying pop-up (alert/builder) when I get a "Yes" answer. But not when I get a "No" answer. In other words, I would like to put an alert.dismiss() in there ... but I can't, because it's not created until the end of the process.
Does that make sense? I'm racking my brain on this trying to find another way to present this to a user.
Here is my code:
public void showGalleriesDialogue(String galleriesArray) {
Log.v("Status","Ran alert dialogue routine.");
final CharSequence[] items = TextUtils.split(galleriesArray, "\\^");
final AlertDialog.Builder builder = new AlertDialog.Builder(MainMenu.this);
builder.setTitle("Choose a Gallery to download:");
builder.setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
final String galleryToDownload = (String) items[item].toString();
// Check to ensure that this doesn't all ready exist
File checkExisists = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/pm-pvault/" + galleryToDownload);
Log.v("Status","Checking to ensure that '"+checkExisists+"' does not exist.");
if(checkExisists.exists()) {
AlertDialog.Builder existsBuilder = new AlertDialog.Builder(MainMenu.this);
existsBuilder.setTitle("But wait!");
existsBuilder.setMessage("This gallery already exists. Do you want to RE-INSTALL it?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Preview stuff, then go get it.
GlobalDataStore.retrycount = 0;
preview(galleryToDownload);
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Never mind.
dialog.cancel();
}
});
AlertDialog existsAlert = existsBuilder.create();
existsAlert.show();
}
//OR ELSE
else
if(galleryToDownload.contains("@")==true)
{
AlertDialog.Builder doItNasty = new AlertDialog.Builder(MainMenu.this);
doItNasty.setTitle("DISCLAIMER");
doItNasty.setMessage(R.string.nastyDisclaimer)
.setCancelable(false)
.setPositiveButton("I AGREE", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Download stuff, go get it.
GlobalDataStore.retrycount = 0;
preview(galleryToDownload);
}
})
.setNegativeButton("DISAGREE", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Never mind.
dialog.cancel();
}
});
AlertDialog nastyAlert = doItNasty.create();
nastyAlert.show();
}
else {
// Out of IF conditions to meet. Go download it now.
GlobalDataStore.retrycount = 0;
preview(galleryToDownload);
}
} // END OR ELSE
}
});
final AlertDialog alert = builder.create();
alert.show();
}
Why not just set a flag when the user selects yes or no, then when the time comes to build the dialog check your flag and if the user selected yes then don't create it. That way you don't have to worry about calling dismiss on it because you won't have made it.
The onClick event is a callback, which means the dialog already exists when it's called, so there should be no problem cancelling it.
The problem is you're redefining the variable 'dialog', so you have no way to access the 'outer' dialog.
Try doing it something like:
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface redownloadDialog, int id) {
// Never mind.
redownloadDialog.cancel();
dialog.cancel();
}
});
精彩评论