Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 176
  • Last Modified:

AlertDialog won't dismiss

Hi,

Using this code, my AlertDialog won't dismiss, I have to hit Ok 3-4 times before it does.

	private void correctDialog(){
		final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
		alertDialog.setTitle("Reset...");
		alertDialog.setMessage("Are you sure?");
		alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				alertDialog.dismiss();
			}
		});
		alertDialog.show();
	}

Open in new window

0
Computer Guy
Asked:
Computer Guy
  • 3
  • 3
1 Solution
 
Rafael ISolutions EngineerCommented:
you don't really have to tell the dialog box to dismiss itself. it just runs whatever code you want and then it dismisses itself.  for example, in one of my apps, i just set up a dialog to alert the user with a message that the list is empty and an OK button.  the rest of my code handles what happens afterwards.

		if(results.isEmpty()) {
			new AlertDialog.Builder(this)
			.setIcon(android.R.drawable.ic_dialog_alert)
			.setTitle(R.string.strEmptyTitle)
			.setMessage(R.string.strEmptyMsg)
			.setPositiveButton(R.string.strDlgOK, new DialogInterface.OnClickListener() {
			        public void onClick(DialogInterface dialog, int whichButton) {
			        	// do nothing
			        }
			}).show();
		}

Open in new window

0
 
Computer GuyAuthor Commented:
Would the dismiss code cause it to make is so I have to hit it 4 times before it closes?
0
 
Rafael ISolutions EngineerCommented:
The thing is, AlertDialog implements Dialog, so you can create a Dialog and customize it a lot, but by calling AlertDialog, you're just making a simple 1, 2, or 3 button alert.

AlertDialog class: http://developer.android.com/reference/android/app/AlertDialog.html
UI Dialog design: http://developer.android.com/design/building-blocks/dialogs.html

so dismiss() is available to AlertDialog, but isn't really used because it handles it on its own.  i believe that is the reason you have to hit it 3-4 times - android doesn't really get why you're trying to dismiss it, throws some warnings, and eventually the alert dialog closes on its own.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Computer GuyAuthor Commented:
I took that like out of my code and it still loops.
0
 
Computer GuyAuthor Commented:
You can grab my project here.
http://bit.ly/1GqycJi
0
 
krakatoaCommented:
I don't do Android, but I'm wondering which setButton method you are calling there . . . some have been deprecated. Do you feel like clarifying that?
0
 
Rafael ISolutions EngineerCommented:
did you take out the command where you call dismiss()? How did it go?
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now