2

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.

Ashok G
  • 121
  • 1
  • 2
  • 6
  • Answer is here http://stackoverflow.com/questions/9654529/handle-url-from-oncreatewindow-web-view/23371661#23371661 – albeee Apr 29 '14 at 17:46

1 Answers1

6

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.

tplaner
  • 8,363
  • 3
  • 31
  • 47
Animesh
  • 1,234
  • 2
  • 9
  • 8
  • this didn't fix for me and i'm not sure it makes sense, if you remove any existing webviews then you're not really supporting multiple windows are you? For me, I selectively do `settings.setSupportMultipleWindows(true)` only when i really need to, so disabling this in a certain case solved the problem for me. – hmac May 09 '18 at 10:58