0
I have created a class to facilitate the use of Dialogs, and I would like to know if it is possible to simplify further.
My class is like this:
public class FVRDialog {
private Activity act;
private Context context;
private AlertDialog dialog;
public FVRDialog(Activity act) {
this.act = act;
}
public boolean Confirm(int icon, String Title, String ConfirmText,
String OkBtn, String CancelBtn, final Runnable OkBtnPress, final Runnable CancelBtnPress) {
dialog = new AlertDialog.Builder(act).create();
dialog.setTitle(Title);
dialog.setMessage(ConfirmText);
dialog.setCancelable(false);
if (icon != 0) { dialog.setIcon(icon); }
dialog.setButton(DialogInterface.BUTTON_POSITIVE, OkBtn,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int buttonId) {
OkBtnPress.run();
}
});
dialog.setButton(DialogInterface.BUTTON_NEGATIVE, CancelBtn,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int buttonId) {
CancelBtnPress.run();
}
});
dialog.show();
return true;
}
public void dismiss() {
dialog.dismiss();
}
}
And to run I’m doing so:
final FVRDialogs fvrdialog = new FVRDialogs(this);
fvrdialog.Confirm(@drawable/ic_pergunta, "Titulo", "Descrição", "Sim","Não",
new Runnable() { public void run() { executarBtnSim(); } },
new Runnable() { public void run() { executarBtnNao(); } });
I would like to simplify without needing to use the runnable, leave if possible more or less like this:
FVRDialogs fvrdialog = new FVRDialogs(this);
fvrdialog.Confirm(@drawable/ic_pergunta, "Titulo", "Descrição", "Sim","Não", executarBtnSim(), executarBtnNao());
Or if there’s any other more interesting way to do it, I’m open to suggestions.
I would further decrease the call to function.
this::executarBtnSim
I find it more worthy of lambda than() -> this.executarBtnSim()
.– Jefferson Quesado
@Jeffersonquesado well noted. I added in the reply
– Leonardo Lima
Thanks friends, it worked well for me. I just have one more question, in the documentation mentioned it explains something about compatibility with
Jack
, what would that be? Could I have future compatibility problems using Java 8??– Fernando VR
What happens is that in fact Android does not give native support to Java 8 before version 24. They use a tool that translates their code to a Java 7-compatible version, a process they call desugaring. The Google team had developed the Jack toolchain for this, but they decided to abandon him for a better solution. This new solution is officially supported by Google and you can use it without fear of compatibility issues.
– Leonardo Lima