0

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?

AndreaF
  • 11,975
  • 27
  • 102
  • 168
  • @codeMagic I cannot do this, the AlertDialog is in another class and is reused for others operations according the passed param – AndreaF May 15 '14 at 02:55
  • Check this post. http://stackoverflow.com/questions/8906269/alertdialogs-setcancelablefalse-method-not-working – Raghu May 15 '14 at 02:59
  • @Raghu the linked post doesn't answer my question – AndreaF May 15 '14 at 03:00
  • Sorry. I did not realize you wanted to block the main thread till user selects an option. Generally blocking the main thread is not a good idea (using wait or any other mechanism). Another approach is pass the activity that is calling show alert as a parameter and call a method from there by defining/implementing an interface and add what the subsequent code there. Hope this helps. – Raghu May 15 '14 at 03:06
  • Check this http://stackoverflow.com/questions/23408756/create-a-general-class-for-custom-dialog-in-java-android/23408864#23408864 – Libin May 15 '14 at 03:09

2 Answers2

0

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.

ZakR
  • 91
  • 1
  • 6
  • there is another solution that doesn't force me to include a whole library only for this operation? – AndreaF May 15 '14 at 03:06
0

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.

DigCamara
  • 5,540
  • 4
  • 36
  • 47
  • Could you give me more details? the Dialogs are in another class, how should I implement `OnDismissListener` to fix my problem ? – AndreaF May 15 '14 at 11:06
  • @AndreaF I added implementation details. You can also see some good examples, for instance here: http://mobiledevtuts.com/android/android-alert-dialog-example-code-tutorial/ – DigCamara May 15 '14 at 14:47
  • Now your suggestion is more clear, but if the Dialog in `showDialog()` contains an `EditText` and I need to get the string input on positive button click, how could I do? – AndreaF May 15 '14 at 19:12
  • Your code doesn't show that case. However, if you do have an EditText, I'd have to assume you used the technique described in the AlertDialog documentation Class Overview(http://developer.android.com/reference/android/app/AlertDialog.html). 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. – DigCamara May 15 '14 at 19:28