Overriding Android WebChromeClient's onCreateWindow method results in SIGSEGV

Ashok G picture Ashok G · Feb 21, 2011 · Viewed 10.7k times · Source

I tried to override the default WebChromeClient in order to get give my application's WebView the ability to open new windows. For this, as instructed in the manual, I'm overriding the 'onCreateWindow' method of WebChromeClient wherein I do the following simple logic.

    public boolean onCreateWindow (WebView view, boolean dialog, boolean userGesture, Message resultMsg) {

        ((WebView.WebViewTransport) resultMsg.obj).setWebView(myWebView);
        Log.d("webviewdemo", "from the chrome client");
        resultMsg.sendToTarget(); 
        return true;
    }

But this results in the above mentioned segmentation fault. I did some search & found that it's already reported at http://code.google.com/p/android/issues/detail?id=11655. I don't see any updates to that issue after that. Does somebody know the status of the same?

Thanks, Ashok.

Answer

Animesh picture Animesh · Jan 12, 2012

The app crashes if you reuse a webview in onCreateWindow.

Instead of webview, use a ViewGroup in the screen layout, give it the same layout parameters (location, size etc) as you gave the webview (mWebViewPopup).

    @Override
    public boolean onCreateWindow(WebView view, boolean dialog, boolean userGesture, android.os.Message resultMsg)
    {
        contentContainer.removeAllViews();

        WebView childView = new WebView(mContext);
        childView.getSettings().setJavaScriptEnabled(true);
        childView.setWebChromeClient(this);
        childView.setWebViewClient(new WebViewClient());
        childView.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        contentContainer.addView(childView);
        WebView.WebViewTransport transport = (WebView.WebViewTransport) resultMsg.obj;
        transport.setWebView(childView);
        resultMsg.sendToTarget();
        return true;
    }

in the above code

1) I have set layout parameters so that my web views fills the parent, you should use layout parameters as per your requirement. 2) mContext => context object 3) contentContainer => viewgroup which was declared in XML intended to contain the web view

This is not clean but solves the problem.