Is it possible to simplify the Runnable command on an Android function?

Asked

Viewed 50 times

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.

1 answer

3


Android Studio 3 supports some features of Java 8. You can enable this by adding the following lines in the build.Radle:

android {
  ...
  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }
}

This way you can pass a lambda as parameter:

fvrdialog.confirm(@drawable/ic_pergunta, "Titulo", "Descrição", 
    "Sim","Não", () -> executarBtnSim(), () -> executarBtnNao());

Or a method reference:

fvrdialog.confirm(@drawable/ic_pergunta, "Titulo", "Descrição", 
    "Sim","Não", this::executarBtnSim, this::executarBtnNao);
  • 1

    I would further decrease the call to function. this::executarBtnSim I find it more worthy of lambda than () -> this.executarBtnSim().

  • @Jeffersonquesado well noted. I added in the reply

  • 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??

  • 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.

Browser other questions tagged

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