How to clear WebView content?

Android

Android 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()?

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionpeterlawnView Question on Stackoverflow
Solution 1 - AndroidGrantland ChewView Answer on Stackoverflow
Solution 2 - AndroidsandaloneView Answer on Stackoverflow
Solution 3 - AndroidKuldip BairagiView Answer on Stackoverflow
Solution 4 - AndroidSudheesh VSView Answer on Stackoverflow
Solution 5 - AndroidAdamView Answer on Stackoverflow
Solution 6 - AndroidHelloWorldView Answer on Stackoverflow