Triggering WebChromeClient onRequestFocus

Issue

I’m trying to trigger WebChromeClient.onRequestFocus() callback. In order to acquire information on when it is called, I’ve dived into android project tests.

I’ve googled up test case of WebChromeClient from 2009 and created following piece of code to reproduce the test behaviour, which seems not to work.

public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final Activity activity  this;
    final WebView webView  (WebView)findViewById(R.id.webView2);

    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
    webView.getSettings().setSupportMultipleWindows(true);

    webView.setWebChromeClient(new WebChromeClient() {
        @Override
        public void onCloseWindow(WebView window) {
            super.onCloseWindow(window);
            Log.i("", " onCloseWindow()");
        }

        @Override
        public boolean onCreateWindow(WebView view, boolean dialog, boolean userGesture,
                                      Message resultMsg) {
            WebView childView  new WebView(activity);
            childView.setWebChromeClient(this);
            WebView.WebViewTransport transport  (WebView.WebViewTransport) resultMsg.obj;
            transport.setWebView(childView);
            resultMsg.sendToTarget();
            Log.i("", " onCreateWindow()");
            return true;
        }

        @Override
        public void onRequestFocus(WebView view) {
            Log.i("", " onRequestFocus()");
        }
    });


    webView.loadData("<html><body onLoad'"
            + "childWindow  window.open();"
            + "childWindow.document.title  \"javascript child window\";"
            + "childWindow.document.write(\"javascript child window\");"
            + "childWindow.focus();"
            + "setTimeout(function(){childWindow.close();}, 10000);"
            + "'><h1>Hello World!</h1></body></html>", "text/html", "UTF-8");
}

Logcat of the debugging session confirms that onCreateWindow() and onCloseWindow() functions are called, no onRequestFocus() call though.

Any ideas?

I use API level 19 (Android 4.4)

Solution

I found the answer. Create webpage with two frames

<html>
<frameset rows20%,80%>
    <frame src"frame1.html" name"myFrame1" />
    <frame src"frame2.html" name"myFrame2" />
</frameset>
</html>

and place a link in frame1.html with target set to myFrame2

<a id"link" href"frame3.html" target"myFrame2">Click me!</a>

Answered By – mkrasowski

Leave a Comment