1

I'm developing a bridge and am able to send a message from JS to Java. But now, I want to send a message back to JS.

I know about a way of doing it using Activity.startActivityForResult(intent, request_code) And then calling webView.evaluateJavascript but I don't want the intent part.

Is there a way to create an empty intent (new Intent() doesn't work) or an alternative to this method?

1
  • 1
    You might consider editing your question to explain exactly what you mean by "a bridge", and to show a minimal reproducible example of how you are sending messages from JavaScript to Java. For example, if this is a WebView and you are using message channels for JS -> Java, you can use the same thing for Java -> JS: commonsware.com/blog/2017/01/23/… Commented May 16, 2021 at 20:45

1 Answer 1

0

You can register an interface in Java with JavaScript through which JavaScript can call your native Java functions. Here you can find more details:

https://developer.android.com/guide/webapps/webview#BindingJavaScript

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

3 Comments

Yes. as per my question, I've already done that. Now I want the opposite. That is, send messages from Java to JavaScript.
Sorry I misread your question. Have you tried calling JS functions like this:. webView.loadUrl("javascript:foo('test message')");
Here foo is the JavaScript function name and we are passing string as parameter.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.