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?
It looks interesting! I’m going to try it out!
– viana
Not your opinion @ramaral, you see this as a "good practice" practice or there are more viable ways?
– viana
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.
– ramaral
Are there "more viable ways"? In this case, called a method, I see no other possibility.
– ramaral
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.
– viana