Skip to content Skip to sidebar Skip to footer

How To Clear The (css) Visited History Of An Android Webview?

I try to reuse an existing WebView by clearing any private data the previous user left behind: CookieManager.getInstance().removeAllCookie(); webview.clearHistory(); webview.clearF

Solution 1:

Override WebChromeClient and WebViewClient... Damn that was hidden.

I actually had to dig up a bit to find this out.

WebView webView = (WebView)findViewById(R.id.myWebView);
WebChromeClient myWebChromeClient = newWebChromeClient(){
        @OverridepublicvoidgetVisitedHistory(ValueCallback<String[]> callback) {
// called during webview initialization, original implementation does strictly nothing // and defaults to the native method WebViewCore.nativeProvideVisitedHistory()String[] myUserHistory = getVisitedUrlsFromMyOwnDatabase(userId);
            callback.onReceiveValue(myUserHistory);
        }
    };
WebViewClient myWebViewClient = newWebViewClient(){
    @OverridepublicvoiddoUpdateVisitedHistory(WebView view, String url,
        boolean isReload) {
// called whenever there is a new link being visitedinsertIfNotExistVisitedUrlIntoMyOwnDatabaseForUser(userId);
        super(view, url, isReload);
}
}
webView.setWebViewClient(myWebViewClient);
webView.setChromeClient(myWebChromeClient);
webView.getSettings().etc(whatever)...

I think I'm "almost there". Here's the part I managed: what it does so far is remove css history altogether, so we're halfway there. I can't get the browser to recognize the url format I'm providing in "myUserHistory", so in effect the only feature this code does is reset css history altogether, but it's only called once when the WebView is instanciated (or created, didn't check), so for a true multiuser experience you'd need to recreate the webview at each login.

My problem now is that I can't manage to load the urlHistory properly. My Honeycomb Xoom webview seems to ignore my data. Ah well, I hope it works for you. For me just calling callback.onReceiveValue(new String[]{}); in getVisitedHistory() will be good enough.

EDIT: I just put twenty more minutes into it because I'm curious. This method is what delegates to the WebChromeClient (mCallbackProxy = WebChromeClient).

protectedvoidpopulateVisitedLinks() {
     ValueCallback callback = newValueCallback<String[]>() {
         publicvoidonReceiveValue(String[] value) {
             sendMessage(EventHub.POPULATE_VISITED_LINKS, (Object)value);
         }
     };
     mCallbackProxy.getVisitedHistory(callback);
 }

It's protected in WebViewCore, which is a private attribute of WebView with no accessor. The sendMessage delegates to EventHub which is private, and WebViewCore is filled with private native methods, and one of these seems to be the one actually calling the populateVisitedLinks() method during the initialization.

Unless someone at Google adds a public method to WebView to trigger the repopulation, I'm afraid it's practically impossible to achieve your goal. Sorry :(

As a side note, all these native visited history handling really makes me wonder: why do hardware manufacturers care so much about which urls we visited? ;) <<< sarcasm

Solution 2:

As an alternate solution, you could try adding your own CSS with the same base colors the default CSS has and switch the CSS by another one (with same color for both "types" of links) when you want to reset the visited links.

A:link{color: "#990000"; text-decoration: none;}
A:visited{color: "#990000"; text-decoration: none;}
A:hover{color: "#ff0000"; text-decoration: none;}

Solution 3:

If you can obtain a Browser instance (maybe you can set a WebChromeClient to WebView) you can use its clearHistory() method.

Solution 4:

Does WebStorage.clearAllData() have the desired effect? Unfortunately, the documentation on this class is very sparse compared to WebView and doesn't say whether it applies to WebViews.

The exact time you're calling clearHistory() may also have an effect. Clearing it and then navigating to a new page may still keep the first page in history, and you have to call the method after the new page has loaded.

Personally, if privacy is a real issue, I would create a new set of objects from scratch for this new session if possible.

Post a Comment for "How To Clear The (css) Visited History Of An Android Webview?"