Passing Code As Parameter
I'm using this code to show messages: Common.getHandler().post(new Runnable(){ public void run(){ Toast.makeText(Common.getContext(), 'Text...', Toast.LENGTH_SHORT).sho
Solution 1:
This is about the best you can do in Java. You can only pass objects (and scalars) around, so you either have to create a class implementing Runnable, or do what you are doing now which is to create an anonymous object instance implementing Runnable.
Solution 2:
Java doesn't have "standalone" closures; passing anonymous implementations is the closest facsimile.
The only real way to make it "shorter" is to create a concrete implementation and pass that instead--but that's only shorter at the call site.
Solution 3:
Java is not the JavaScript.
Your proposition is not possible, as you cannot pass methods and functions as such easily. the code at the top could be simplified to (with appropriate change to your Common class):
Common.run(new Runnable() {
publicvoidrun() {
// your code
}
}
Post a Comment for "Passing Code As Parameter"