Here is my Javascript interface:
class WebAppInterface(val context: Context){
@JavascriptInterface
fun provideAString():String{
println("executed from js")
println("this is text")
return "this is text"
}
@JavascriptInterface
fun printTheString(s:String){
println(s)
}
}
Here's how I add it to the WebView:
webView.addJavascriptInterface(WebAppInterface(this), "Android")
Here's how I call the two functions from JS:
function StringFromAndroid(){
var string=Android.provideAString();
}
...
...
var currentString=new StringFromAndroid();
Android.printTheString(currentString.string);
Here are the print lines:
I/System.out: executed from js
I/System.out: this is text
I/System.out: undefined
Expected behavior:
Print lines 1 and 2 demonstrate that provideAString() is being called correctly from JS. Print line 3 shows that printTheString(s) is being called correctly from JS, but that the value of currentString.string inside the WebView is "undefined". The expected behavior is that when var currentString=new StringFromAndroid(); forces a call to Android.provideAString(); via the line var string=Android.provideAString();, that call would return the string "this is text" and set var string to have that value so that resultingly, currentString.string would be "this is text" instead of "undefined".