Solved

AlertDialog won't dismiss

Posted on 2014-12-15
7
131 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 3

Author Comment

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

Author Comment

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

Expert Comment

by:krakatoa
Comment Utility
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
Comment Utility
did you take out the command where you call dismiss()? How did it go?
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

This is an explanation of a simple data model to help parse a JSON feed
If your app took Google’s lash recently, here are the 5 most likely reasons.
This video teaches viewers about errors in exception handling.
This video is in connection to the article "The case of a missing mobile phone (https://www.experts-exchange.com/articles/28474/The-Case-of-a-Missing-Mobile-Phone.html)". It will help one to understand clearly the steps to track a lost android phone.

763 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now