I have a simple browser app, my onCreateWindow
looks like this:
if (userGesture) {
WebView childView = new WebView(webBrowserActivity);
WebSettings settings = childView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setSupportZoom(true);
settings.setJavaScriptCanOpenWindowsAutomatically(true);
settings.setBuiltInZoomControls(true);
settings.setSupportMultipleWindows(true);
settings.setLoadWithOverviewMode(true);
settings.setUseWideViewPort(true);
childView.addJavascriptInterface(new MyInterface(),"NAME");
<--- code in here to add the view to a ViewPager.
WebView.WebViewTransport transport = (WebView.WebViewTransport) resultMsg.obj;
transport.setWebView(childView);
resultMsg.sendToTarget();
Log.v(TAG, "Opening popup for " + childView.getUrl());
webBrowserActivity.invalidateOptionsMenu();
return true;
} else {
return false;
}
I have tried adding this interface a bit later down the line, I have also tried calling reload()
or loadUrl()
after the page is loaded because the documentation says the javascript interface must be added prior to loading the page. None of this has helped. Has anyone done this successfully?
Thanks.
EDIT: I should mention I see this more on 4.4, I am using the desktop debugger for Chrome and I can go into the console and see the window.NAME
object on the first webview and not on the second one. On 4.3 it seems to work fairly often.
I edited the code above to show the settings I am using. The javascript interface I am adding to the WebView works just fine when added to the first WebView that gets created when the Activity starts.
Unfortunately there is a bug in the KitKat WebView that makes it impossible to register a JavaScript interface on the popup window. There is no known workaround for the issue I'm afraid, other than as you suggest, keep the URL for the popup, return false from onCreateWindow and then create a whole new webview and load the pop up URL into it.