Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I detect when a WebView page is trying to close?

I have a WebView into which I'm loading the facebook sharer php page. This page doesn't have any form of confirmation, it simply closes the window once the user has either shared or cancelled.

Since it is loaded into a web view there is nothing to "close" perse, so, I need to detect the window trying to close and act on that callback.

From my various searches it appears that the way to handle this is via a WebChromeClient. So, I'm attempting the following, but the callback is never called. I am obviously missing something, but don't know what. It *feels like the onCloseWindow event should be Overriden, but that causes eclipse to complain (reasonably) that I need to super the instance, which the documentation doesn't seem to suggest is possible.

All help appreciated.

[EDIT] oh, and I just tried swapping the order of the actions, setting the WebChromeClient before calling the url, and (as I expected) that didn't alter the behavior in any way.

webview.getSettings().setJavaScriptEnabled(true);
    webview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true); 
    webview.setWebViewClient(new WebViewClient()); 
    webview.loadUrl("http://www.facebook.com/sharer/sharer.php?u=http://EXTRACTED.com?img_id=abc");// + getIntent().getStringExtra("userId"));
    WebChromeClient wbc = new WebChromeClient(){

        public void onCloseWindow(Window w){
            Log.d(TAG, "Window trying to close");
        }
    };

    webview.setWebChromeClient(wbc);
like image 808
Yevgeny Simkin Avatar asked Aug 23 '11 17:08

Yevgeny Simkin


People also ask

How do you listen for a WebView finishing loading URL?

WebViewClient onPageFinished() | listen for a WebView finishing loading a URL. To know about the URL loaded or not in WebView have to use the webViewClient onPageFinished() method. A WebView to have a WebViewClient to make use of the onPageFinished event callback.

How do I close WebView?

Add a close button and on its click set: webview. setVisibility(View. INVISIBLE); webview.

How can I make WebView keep a video or audio playing in the background?

I did something similar: Just extend WebView and override onWindowVisibilityChanged . This way, the audio continues to play if the screen is locked or another app is opened. However, there will be no controls in the notification bar or on the lockscreen.

Is WebView slower than browser?

Web View is slower on both as compared to native android browser.


1 Answers

Whats wrong with:

   public void onCloseWindow(WebView w){
        super.onCloseWindow(w);
        Log.d(TAG, "Window trying to close");
    }
like image 57
Blundell Avatar answered Nov 15 '22 02:11

Blundell