lwc:programming:mobile:android:lambdas_and_friends

Treating methods as first-class objects requires lambda functions. Very basic example:

public class MainActivity extends AppCompatActivity {
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_main);
        testTheThings();
    }
 
    // to create a basic lambda function you need to have an interface for it.
    // that serves as a template for what the function will have as inputs and outputs:
    interface MyInterface {
        int doTheThings(int i);
    }
 
    // now we can assign a variable to that interface:
    MyInterface myInterface = (x -> myFunction(x));
 
    // myFunction must be defined
    int myFunction(int i) {
        return i + 1;
    };
 
    // and we can call the function through the interface and print the result
    void testTheThings() {
        int result = myInterface.doTheThings(4);
        Log.v("javademo2", "having done the things resulted in " + result);
    }
};
  • lwc/programming/mobile/android/lambdas_and_friends.txt
  • Last modified: 2022/02/23 12:59
  • by John Harrison