Solved

AlertDialog won't dismiss

Posted on 2014-12-15
7
146 Views
Last Modified: 2015-01-05
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
Comment
Question by:Computer Guy
  • 3
  • 3
7 Comments
 
LVL 6

Expert Comment

by:Rafael I
ID: 40502733
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
 
LVL 3

Author Comment

by:Computer Guy
ID: 40502747
Would the dismiss code cause it to make is so I have to hit it 4 times before it closes?
0
 
LVL 6

Accepted Solution

by:
Rafael I earned 500 total points
ID: 40502841
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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 3

Author Comment

by:Computer Guy
ID: 40503089
I took that like out of my code and it still loops.
0
 
LVL 3

Author Comment

by:Computer Guy
ID: 40503093
You can grab my project here.
http://bit.ly/1GqycJi
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 40504538
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
 
LVL 6

Expert Comment

by:Rafael I
ID: 40507731
did you take out the command where you call dismiss()? How did it go?
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
moto 4g plus memory usage 30 46
hibernate example using maven 12 59
CSV file parsing thru Java 13 34
spring maven example issues 3 13
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will learn how to implement Singleton Design Pattern in Java.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question