?
Solved

AlertDialog won't dismiss

Posted on 2014-12-15
7
Medium Priority
?
160 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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
Industry Leaders: 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!

 
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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

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 …
A short article about problems I had with the new location API and permissions in Marshmallow
The viewer will learn how to implement Singleton Design Pattern in Java.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month10 days, 23 hours left to enroll

770 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