Question

In a my Activity I call a method that shows an alert dialog and returns a response according the button clicked on the AlertDialog and others checks (this method is in another class)

something like:

 public static boolean showAlertDialog(Context c,int param){
...
AlertDialog.Builder alert = new AlertDialog.Builder(con);
        alert.setTitle(title);
        alert.setIcon(android.R.drawable.ic_dialog_info);
        alert.setMessage(message);
        alert.setPositiveButton(R.string.yes,
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,
                                        int whichButton) {
                        ...
                    }
                }


           );

        ....
        alert.show();
        return status;

    }

The problem is that when I call this method the code doesn't wait that the user do his operation on AlertDialog, but continues the execution.

so if in my activity do

boolean status=false;
status=Alerts.showAlertDialog(this);

if(status){
   //do this
}
else{
  //do that
}

always the else block is executed

How should I solve this?

Was it helpful?

Solution

The simplest part is actually described in the Developer's Guide for Android, in the Dialogs tutorial.

It's pretty simple: you have to implement an interface on the Activity's side (implements NoticeDialogFragment.NoticeDialogListener) and override the callback methods

@Override
public void onDialogPositiveClick(DialogFragment dialog) {
    // User touched the dialog's positive button
    ...
}

@Override
public void onDialogNegativeClick(DialogFragment dialog) {
    // User touched the dialog's negative button
    ...
}

The example I'm giving uses the DialogFragment which doesn't exactly fit your code. However, the implementation isn't that different for you, you just have to look up the corresponding interfaces (the OnDismissListener for instance)

You currently have this:

boolean status=false;
status=Alerts.showAlertDialog(this);

if(status){
   //do this
}
else{
  //do that
}

You'll have to eliminate the whole if. Change your Activity so it implements the DialogInterface.OnClickListener interface

public class Blah extends Activity implements DialogInterface.OnClickListener

and add the method

public void onClick(DialogInterface dialog, int which){
   if (which==DialogInterface.BUTTON_POSITIVE){
        //TODO: do your stuff
   }
}

finally, in your Alerts.showDialog() method change

 alert.setPositiveButton(R.string.yes,
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog,
                                    int whichButton) {
                    ...
                }
            }


       );

to

 alert.setPositiveButton(R.string.yes,c);

If you do have an EditText, I'd have to assume you used the technique described in the AlertDialog documentation Class Overview. If so, since you're receiving the dialog as a parameter, you would have to find the view in the Dialog with findViewById and extract the value that way.

OTHER TIPS

You could use EventBus. This would allow you to post an event from the AlertDialog that you could handle in as many fragments or activities as you want.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top