To explain what I want to achieve I wrote what I'm able to do and then - when a problem appears. What works well:
1) I have webView1
containing in loaded HTML
<button onclick="android.buttonPressed();">Click</button>
binded with interface:
private class WebAppInterface1 {
Context context;
WebAppInterface1(Context c) {
context = c;
}
@JavascriptInterface
public void buttonPressed() {
Toast.makeText(context, "buttonPressed", Toast.LENGTH_LONG).show();
}
}
The HTML button works.
2) I have webView2
containing in loaded HTML
<script>
function f() {
document.write("bla bla bla");
}
</script>
Then I call
webView2.loadUrl("javascript:f();");
in android code and it works too.
Now I want to modify webView2
's page by pressing button in webView1
. Technically it means that I want to call f()
inside webView2
when JS interface detected click in webView1
. So I add this line
webView2.loadUrl("javascript:f();");
in buttonPressed()
function inside JS interface. But it has no effect. What can cause the problem? Are there any limitations in connecting these reciever and sender? Maybe I missed some settings or permissions?
There are my project files, I tried to make them as simple as possible: layout, html, java. Didn't edit other files. Can give any additional information if needed.