Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android AlertDialog Box Not Showing

I've searched through Stackoverflow, looked at the examples in Android Developer, and some books and I'm just not getting it.

I'm trying to show an AlertDialog Box that interrupts the program flow. Most of the examples I've seen don't have code after the dialog box and I need my program to stop executing until the AlertDialog button is pressed. Seems as if the AlertDialog is created from another thread and I'm not sure how to retrieve that thread.

Program logic: If the parsing is bad the program will force close. I want to let the user know to restart the program and everything will work. (I'm dropping and recreating tables and they are repopulated when the program starts back up)

Here's some code:

if(database populated)
{
  ....code.....

  if(parseok.contentEquals("Error"))
  {
    doForceClose();
  }
displayDate = "Last: " + parseok;  //I don't want the program to continue to here.
//Rest of code in the method.  If I continue the program will Force Close
}
else
  do something else

Here's the AlertDialog method:

private void doForceClose()
{
  String themessage = "Because some of the parameters have changed in the yada yada"; 

  AlertDialog.Builder ad = new AlertDialog.Builder (this);
  ad.setTitle("Internal Error");
  ad.setMessage(themessage);
  ad.setPositiveButton("Sorry", new OnClickListener()
  {
    public void onClick(DialogInterface dialog, int which)
    {
    finish();
    return;
    }
  });
  ad.create();
  ad.show();        
}

except ad doesn't show and the program continues to its force close.

Obviously I'm not getting something. Any ideas?

edit: I am in a Class that extends Activity

like image 552
eric Avatar asked Feb 26 '23 01:02

eric


2 Answers

I'm trying to show an AlertDialog Box that interrupts the program flow.

That does not exist in Android and various other UI systems.

I need my program to stop executing until the AlertDialog button is pressed.

No, you don't. Event-driven programming has been in use for a couple of decades.

Program logic: If the parsing is bad the program will force close.

That's your code -- rewrite it to behave better.

I don't want the program to continue to here.

Then use an else, or a return, or something.

except ad doesn't show and the program continues to its force close.

Your dialog will not appear until the main application thread gets control again to process your request -- show() is asynchronous. You are crashing before then, most likely.

In short, your strategy for dealing with your parsing problem is fundamentally flawed.

like image 183
CommonsWare Avatar answered Mar 07 '23 05:03

CommonsWare


The Commonsware response s correct. Let me try and say the same thing in different words. An alert dialog does NOT interrupt the flow of control. It is just "left showing" when the program is waiting for input.

thus the sequence showAlert("this is a message); showGallery(); return;

this shows only momentarily. A way out of this is to put the showGallery() function call inside the Positive response from the AlertDialog.

So to put it another way. If you want to interrupt the flow of your app with an AlertDialog (which is wisely pointed out is the wrong thing to want) then put the code you want executed after the dialog into the onClick callback of the AlertDialog.

like image 36
user462990 Avatar answered Mar 07 '23 04:03

user462990