Passing function as a parameter in java Passing function as a parameter in java android android

Passing function as a parameter in java


Use a callback interface or an abstract class with abstract callback methods.

Callback interface example:

public class SampleActivity extends Activity {    //define callback interface    interface MyCallbackInterface {        void onDownloadFinished(String result);    }    //your method slightly modified to take callback into account     public void downloadUrl(String stringUrl, MyCallbackInterface callback) {        new DownloadWebpageTask(callback).execute(stringUrl);    }    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        //example to modified downloadUrl method        downloadUrl("http://google.com", new MyCallbackInterface() {            @Override            public void onDownloadFinished(String result) {                // Do something when download finished            }        });    }    //your async task class    private class DownloadWebpageTask extends AsyncTask<String, Void, String> {        final MyCallbackInterface callback;        DownloadWebpageTask(MyCallbackInterface callback) {            this.callback = callback;        }        @Override        protected void onPostExecute(String result) {            callback.onDownloadFinished(result);        }        //except for this leave your code for this class untouched...    }}

The second option is even more concise. You do not even have to define an abstract method for "onDownloaded event" as onPostExecute does exactly what is needed. Simply extend your DownloadWebpageTask with an anonymous inline class inside your downloadUrl method.

    //your method slightly modified to take callback into account     public void downloadUrl(String stringUrl, final MyCallbackInterface callback) {        new DownloadWebpageTask() {            @Override            protected void onPostExecute(String result) {                super.onPostExecute(result);                callback.onDownloadFinished(result);            }        }.execute(stringUrl);    }    //...


NO interface, NO lib, NO Java 8 needed!

Just using Callable<V> from java.util.concurrent

public static void superMethod(String simpleParam, Callable<Void> methodParam) {    //your logic code [...]    //call methodParam    try {        methodParam.call();    } catch (Exception e) {        e.printStackTrace();    }}

How to use it:

 superMethod("Hello world", new Callable<Void>() {                public Void call() {                    myParamMethod();                    return null;                }            }    );

Where myParamMethod() is our passed method as parameter (in this case methodParam).


Yes, an interface is the best way IMHO. For example, GWT uses the command pattern with an interface like this:

public interface Command{    void execute();}

In this way, you can pass function from a method to another

public void foo(Command cmd){  ...  cmd.execute();}public void bar(){  foo(new Command(){     void execute(){        //do something     }  });}