Home » Android » java – How can i pass a function as a parameter to another function in android?

java – How can i pass a function as a parameter to another function in android?

Posted by: admin June 15, 2020 Leave a comment

Questions:

So how can I pass a function as a parameter to another function, for example i want to pass this function:

public void testFunkcija(){
    Sesija.forceNalog(reg.getText().toString(), num);
}

in this:

    public static void dialogUpozorenjaTest(String poruka, Context context, int ikona, final Method func){
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
            context);
        alertDialogBuilder.setTitle("Stanje...");
        alertDialogBuilder
            .setMessage(poruka)
            .setIcon(ikona)
            .setCancelable(true)                        
            .setPositiveButton("OK",new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog,int id) {
                    //here
                }
              });

        AlertDialog alertDialog = alertDialogBuilder.create();
        alertDialog.show();
}
How to&Answers:

You can use a Runnable to wrap your method:

Runnable r = new Runnable() {
    public void run() {
        Sesija.forceNalog(reg.getText().toString(), num);
    }
}

Then pass it to your method and call r.run(); where you need it:

public static void dialogUpozorenjaTest(..., final Runnable func){
    //.....
        .setPositiveButton("OK",new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,int id) {
                func.run();
            }
          });
}

Answer:

Well, since there are no dellegates in Java (oh C# I miss you so bad), the way you can do it is creating a class that implements a interface, maybe runnable or some custom interface and than you can call your method through the interface.

Answer:

Functions cannot be passed directly themselves. You could use an interface implementation as a callback mechanism to make the call.

Interface:

public interface MyInterface {

   public void testFunkcija();
}   

Implementation:

public class MyInterfaceImpl implements MyInterface 
   public void testFunkcija(){
       Sesija.forceNalog(reg.getText().toString(), num);
   }
}

and pass it a MyInterfaceImpl instance as required to:

public static void dialogUpozorenjaTest(MyInterface myInterface, ...)

   myInterface.testFunkcija();
   ...

Answer:

the simplest way is using runnable
let’s see how

//this function can take function as parameter 
private void doSomethingOrRegisterIfNotLoggedIn(Runnable r) {
    if (isUserLoggedIn())
        r.run();
    else
        new ViewDialog().showDialog(MainActivity.this, "You not Logged in, please log in or Register");
}

now let’s see how can I pass any function it it (I will not use lambda expression)

Runnable r = new Runnable() {
                @Override
                public void run() {
                    startActivity(new Intent(MainActivity.this, AddNewPostActivity.class));
                }
            };
doSomethingOrRegisterIfNotLoggedIn(r);

let’s pass another function

Runnable r = new Runnable() {
                @Override
                public void run() {
                    if(!this.getClass().equals(MyProfileActivity.class)) {
                        MyProfileActivity.startUserProfileFromLocation( MainActivity.this);
                        overridePendingTransition(0, 0);
                     }
                }
            };
doSomethingOrRegisterIfNotLoggedIn(r);

thas’s it. happy big thinking…