Return values using Dialog’s

Asked

Viewed 275 times

1

I have the following method dialogConfirme in which it contains a custom dialog declared as public static to return a value of the type boolean. The issue of static 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 a following condition in another class and would like the value to return at the moment I click the button:

boolean dialog = dialogConfirme;

if(dialog){

//retornou verdadadeiro ..

} else {

//retornou falso

}

However, before clicking on any button within the dialog method, I already get a return. There would be a way to get the return only the moment I click the buttons btnSim or btnNao? How? It would be a "bad practice" to follow this thought?

2 answers

2


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

Declare a interface:

public interface DialogResultListener{

    void onResult(boolean result);
}

Do 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 like this:

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

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

        //Botão SIM

        } else {

        //Botão NÃO

        }
    }
});
  • It looks interesting! I’m going to try it out!

  • Not your opinion @ramaral, you see this as a "good practice" practice or there are more viable ways?

  • It is a common practice to receive values asynchronously or to personalize behavior. For example, the method could receive an interface with two methods, one to be executed when the SIM button is chosen and the other for the NO button. The behavior of each button can be customized in each use of the method.

  • Are there "more viable ways"? In this case, called a method, I see no other possibility.

  • 1

    It worked @ramaral... my other question was whether it is even feasible to do this type of programming, checking the dialogue return. But like you said, I don’t see anything wrong with using the method this way either. I use this dialog at least 7 times in my code, however when I click yes, I have to do different actions and until then I was unable to solve and save code. Thanks for the help. Hugs.

0

on the line

myDialog.show();

is running the button even if there is no click. as soon as you instantiate the object it performs it put this Cod inside the click solving functions.

  • That doesn’t solve the problem!

  • he keeps running ?

  • i think the problem is in its way of use. Voce creates an object that returns at the time of the instance a value. Why don’t you create the button function by displaying the dialog ? and setting the flag variable? I’m sorry I didn’t fully understand what happens if you can explain what you’re doing by putting the other classes involved help more.

  • It is that I am calling this method with dialogue in various classes always with this decision. If I click yes do something, if I click no, do nothing. But then when I call the method inside the if, it already returns without me clicking the button.

  • yes poios vc instates the object and at the time of the change it already returns 2 things without you doing anything the myDialog.show(); and the Return flag; tries to set the flag as false inside the class and does not use Static

Browser other questions tagged

You are not signed in. Login or sign up in order to post.