4

I have a kotlin method as follows:

fun methodWithCallBackAsParameter(callback:() -> Unit) {
    //Do Somejob
    callback()
}

In kotlin we simply use this method with following syntax:

methodWithCallBackAsParameter {
    //Some Instructions here.
}

Now I want to use this Kotlin method in a Java class but I am not able to figure out how.

5
  • Doesn't methodWithCallbackAsParameter(() -> { }); work? Commented Nov 16, 2018 at 13:14
  • It says missing return statement. Commented Nov 16, 2018 at 13:16
  • I tried returning Unit.INSTANCE; it now says, Lambda expressions are not supported at language level 7. Commented Nov 16, 2018 at 13:19
  • Possible duplicate of Is it possible to use Java 8 for Android development? Commented Nov 16, 2018 at 13:58
  • 1
    If you are using Android Studio 3.0+ then you just need to enable Java 8 language features by copy-pasting a block of code into your build.gradle, see developer.android.com/studio/write/java8-support Commented Nov 16, 2018 at 14:02

5 Answers 5

6

Just return Unit.INSTANCE in the end of the lambda expression.

Sign up to request clarification or add additional context in comments.

2 Comments

I tried returning Unit.INSTANCE; it now says, Lambda expressions are not supported at language level 7.
Either set language level to 1.8 or transform the lambda into anonymous class. This error occurred because lambdas are supported from Java 1.8, and your project uses Java 1.7.
1

if you want to use lambda in android you have to change your app to use Java 1.8 instead of 1.7 use can do that by it from gradle

android {
  ...
  // Configure only for each module that uses Java 8
  // language features (either in its source code or
  // through dependencies).
  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }
}

for more look this documentation https://developer.android.com/studio/write/java8-support

Comments

0

I believe you want to do this.

    public class Program {

    public static void main(String[] args) {
        FunctionKt.methodWithCallBackAsParameter(() -> {
            System.out.printf("do something");
            return null;
        });
    }
}

In this example, the function methodWithCallBackAsParameter was defined in the function.kt file.

Comments

0

You can do something like this:

Supposing the kotlin method is

fun handleResult(
    data: Intent?,
    callbackSuccess: (String) -> Unit,
    callbackError: (String) -> Unit
)

Then when you call this from Java you call it like this (either by matching the signature or doing some logic inside)

    handleResult(data, this::success, this::failure);

    handleResult(data, (statusCode) -> success(statusCode), (errorMessage) -> failure(errorMessage));


    private Unit failure(String errorMessage) {
        // do something java here
        return Unit.INSTANCE;
    }

    private Unit success(String code) {
        // do something java here
        return Unit.INSTANCE;
    }

Comments

-1

When use in Java: callback: () -> Unit = new Function0<>()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.