2

In java you can pass functions as parameters, what if you wanted to pass a function to another activity? Is this possible for example using Intent.putExtra?

Chicowitz
  • 5,759
  • 5
  • 32
  • 38
  • 2
    can you provide an example of what you mean because I think you may be confused about what you mean – tyczj Dec 12 '13 at 21:29
  • I thought there were strategies to accomplish this? http://stackoverflow.com/questions/4685563/how-to-pass-a-function-as-a-parameter-in-java – Chicowitz Dec 12 '13 at 21:30
  • 2
    Yes, you can use that strategy. That is what I reference in my answer below. Just to be clear however, it is not the function that is being passed, it is an object that implements a specific function of interest. – EJK Dec 12 '13 at 21:33
  • Thank you. I think it will be helpful for people that assume this to read my question and your responses – Chicowitz Dec 12 '13 at 21:38

1 Answers1

4

"In java you can pass functions as parameters, ...". This is incorrect. Java does not support this.

You can pass an object of a particular interface and then call that interface method on the object. As long as you make this particular interface either serializable or Parcelable, then you can pass an instance of it in an intent.

EJK
  • 12,332
  • 3
  • 38
  • 55
  • An implementation resp. instance of a `Callable` interface is something you could pass in via a `Parcelable`: http://docs.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/Callable.html – Trinimon Dec 12 '13 at 21:34
  • You basically can in Java 8, which is the upcoming release. See http://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html and http://docs.oracle.com/javase/tutorial/java/javaOO/methodreferences.html – Kelsey Francis Dec 12 '13 at 21:38