Question: I am trying to include an AlertDialog builder within a method that prompts for a pin code and when the positive button is pressed, checks it against a database value and returns a true or false value to the method caller.

For example: Adding/editing/deleting a user task requires a pin code. I don't want to generate a different AlertDialog for all three (and more) of these actions. I want to wrap the following code within a TaskService class that I can call from any activity, and react based on the result from within that activity.

So TaskService.java would have: 

```
public boolean isCorrectPin(View v){
    AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());

    final EditText editText = new EditText(context);
    builder.setView(editText);

    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            if (editText.getText().toString()) == getPinCode(){
                //return true
            }
        }
    });

    builder.show();
}

```

and OpenTaskAdapter.java would have:

```
public void onBindViewHolder(ViewHolder holder, int position){
    holder.btnMarkAsComplete.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v){
            if (service.isCorrectPin(v) {
                //complete task
            }
        }
    });
    holder.btnDelete.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v){
            if (service.isCorrectPin(v) {
                //delete task
            }
        }
    });

```

It's important to note that these two button listeners could be in totally different activities.

Answer: 
A dialog can't "return" a value in the way that it looks like you're expecting. A dialog can make changes to some other object, but you can't have a bit of code block on it and wait for the user to finish interacting with it.

Instead, you'll need to set up listeners for when the prompt dialog is [dismissed](http://developer.android.com/reference/android/app/Dialog.html#setOnDismissListener(android.content.DialogInterface.OnDismissListener)) or buttons or clicked, or whatever other event signals that you have what you need from it. Those listeners can then read the data gathered and set by the dialog.
this is how i'm doing :

```
  public Boolean showAlert(String message)
{
    action = false;

    AlertDialog.Builder alertDialog = new AlertDialog.Builder(HAActivity.this);

    // Setting Dialog Title
    alertDialog.setTitle(getString(R.string.app_name));

    // Setting Dialog Message
    alertDialog.setMessage(message);

    // Setting Icon to Dialog

    // Setting Positive "Yes" Button
    alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog,int which) {

            // Write your code here to invoke YES event
            action = true;
        }
    });

    // Setting Negative "NO" Button
    alertDialog.setNegativeButton("Cancle", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            // Write your code here to invoke NO event
            action = false;
            dialog.cancel();
        }
    });

    // Showing Alert Message
    alertDialog.show();

    return action;
}

```

and calling function like this :
//activity in which you create function

```
 if (Activity.showAlert("Do you really want to delete ??"))
            {
               //delete it anyway.

            }

```