Solved

AlertDialog won't dismiss

Posted on 2014-12-15
7
154 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 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
Technology Partners: 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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

729 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