Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin use Java callback interface

I have a WebView. I want to call

public void evaluateJavascript(String script, ValueCallback<String> resultCallback)

this method.

Here is the ValueCallback interface:

public interface ValueCallback<T> {
    /**
     * Invoked when the value is available.
     * @param value The value.
     */
    public void onReceiveValue(T value);
};

Here is my kotlin code:

webView.evaluateJavascript("a", ValueCallback<String> {
            // cant override function
        })

Anyone have idea to override the onReceiveValue method in kotlin? I tried the "Convert Java to Kotlin" but result is the next:

v.evaluateJavascript("e") {  }

Thanks!

like image 765
vihkat Avatar asked May 26 '17 18:05

vihkat


People also ask

How does callback work in Kotlin?

Android callbacks allow your method to fetch results from another method synchronously. Callbacks act as messengers; they are the center of communication in a program, and get passed into functions as arguments. They help in completing the task of a function by providing results, either negative or positive.

How do I return a callback from Kotlin?

You can use coroutines to return a value from function which has asyn calls in it. You can use interface callbacks to activity/ fragment to trigger the updates received from retrofit calls.

What is object callback in Kotlin?

kotlin.Any. ↳ android.telecom.Call.Callback. Defines callbacks which inform the InCallService of changes to a Call . These callbacks can originate from the Telecom framework, or a ConnectionService implementation.


2 Answers

The following line is called a SAM conversion:

v.evaluateJavascript("e", { value ->
  // Execute onReceiveValue's code
})

Whenever a Java interface has a single method, Kotlin allows you to pass in a lambda instead of an object that implements that interface.

Since the lambda is the last parameter of the evaluateJavascript function, you can move it outside of the brackets, which is what the Java to Kotlin conversion did:

v.evaluateJavascript("e") { value ->
  // Execute onReceiveValue's code
}
like image 161
nhaarman Avatar answered Oct 18 '22 12:10

nhaarman


You already are. The content between your braces is the content of the onReceive function. Kotlin has automatic handling for SAM conversions from Java. All of the following are equivalent.

// Use Kotlin's SAM conversion
webView.evaluateJavascript("a") {
    println(it)  // "it" is the implicit argument passed in to this function
}

// Use Kotlin's SAM conversion with explicit variable name
webView.evaluateJavascript("a") { value ->
    println(value)
}

// Specify SAM conversion explicitly
webView.evalueateJavascript("a", ValueCallback<String>() {
    println(it)
})

// Use an anonymous class
webView.evalueateJavascript("a", object : ValueCallback<String>() {
    override fun onReceiveValue(value: String) {
        println(value)
    }
})
like image 31
Ruckus T-Boom Avatar answered Oct 18 '22 12:10

Ruckus T-Boom