Return values using Dialog's

1

I have the following dialogConfirme method in which it contains a custom dialog declared as public static to return a value of type boolean . The static issue is so I can call any class using a context as shown below:

static boolean flag = false;

public static boolean dialogConfirme(final Context context, String mensagem) {

    final Dialog myDialog = new Dialog(context);
    myDialog.setContentView(R.layout.material_dialog_exit);
    myDialog.setTitle(mensagem);

    Button btnSim = (Button) myDialog.findViewById(R.id.btnOption1);
    btnSim.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            flag = true;
            myDialog.dismiss();
        }
    });

    Button btnNao = (Button) myDialog.findViewById(R.id.btnOption2);
    btnNao.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            flag = false;
            myDialog.dismiss();
        }
    });
    myDialog.show();
    return flag;
}

Doubt

Having the following condition in another class and would like the value to return the moment I clicked the button:

boolean dialog = dialogConfirme;

if(dialog){

//retornou verdadadeiro ..

} else {

//retornou falso

}

However, before clicking any button inside the dialog method, I already get a return. Would I be able to get the return only the moment I click the btnSim or btnNao buttons? As? Would it be a "bad practice" to follow this thought?

    
asked by anonymous 06.09.2016 / 16:52

2 answers

2

Dialog is presented asynchronously, its result (function of the button chosen to close it) can only be obtained by calling callback :

Declare an interface :

public interface DialogResultListener{

    void onResult(boolean result);
}

Make the method dialogConfirme() receive an argument that implements the interface, it will be used to inform the result of the Dialog:

public static void dialogConfirme(final Context context, String mensagem,
                                  DialogResultListener listener) {

    final Dialog myDialog = new Dialog(context);
    myDialog.setContentView(R.layout.material_dialog_exit);
    myDialog.setTitle(mensagem);

    Button btnSim = (Button) myDialog.findViewById(R.id.btnOption1);
    btnSim.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            //Notifique o resultado
            listener.onResult(true);
            myDialog.dismiss();
        }
    });

    Button btnNao = (Button) myDialog.findViewById(R.id.btnOption2);
    btnNao.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            //Notifique o resultado
            listener.onResult(false);
            myDialog.dismiss();
        }
    });
    myDialog.show();
}

Use this:

dialogConfirme(context, "Confirme por favor", new DialogResultListener(){

    public void onResult(boolean result){
        if(result){

        //Botão SIM

        } else {

        //Botão NÃO

        }
    }
});
    
06.09.2016 / 17:20
0

on the line

myDialog.show();

You are running the button even if you do not click. as soon as you instantiate the object it executes this put this cod inside the click functions that resolves.

    
06.09.2016 / 16:58