How to clear WebView content?
AndroidAndroid Problem Overview
I have a WebView
in my app with wrap_content
width and height
Before I use webview.loadData
, the width and height of the it was 0, after I load a page (or I use webview.loadData
) , it'll display a web page.
My question is , how can I clear the webview to recover it back to the original state, just as before it loadData
?
Android Solutions
Solution 1 - Android
I found this answer, pretty helpful: https://stackoverflow.com/questions/4632590/how-do-i-clear-a-webviews-content-before-loading-a-page
It is for Cocoa, but basically just call:
webView.loadUrl("about:blank")
Solution 2 - Android
The only complete solution is to check for SDK version.
if (Build.VERSION.SDK_INT < 18) {
webView.clearView();
} else {
webView.loadUrl("about:blank");
}
Solution 3 - Android
use following statement before loadData:
view.loadUrl("javascript:document.open();document.close();");
Solution 4 - Android
WebView.clearView();
This will clear the view from the webview.
Solution 5 - Android
I used the clearView() method, however I'm now having issues that when I loadData() right after it, it doesn't seem to load the data.
Solution 6 - Android
What about loadUrl("about:blank"), and then clearHistory()?