lwc:programming:mobile:android:lambdas_and_friends

This is an old revision of the document!


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.1645642674.txt.gz
  • Last modified: 2022/02/23 12:57
  • by John Harrison