Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear WebView content?

Tags:

android

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?

like image 342
peterlawn Avatar asked Sep 15 '10 07:09

peterlawn


People also ask

What is WebView content?

Android WebView is a system component for the Android operating system (OS) that allows Android apps to display content from the web directly inside an application.

How do I close WebView on Android?

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

How do I exit WebView?

If you have only 1 activity you can simply start the service and then call finish() on the activity. However, if you have multiple activities you have to make sure that you close them all (see also this post).


2 Answers

I found this answer, pretty helpful: How do I clear a WebView's content before loading a page?

It is for Cocoa, but basically just call:

webView.loadUrl("about:blank") 
like image 67
Grantland Chew Avatar answered Sep 27 '22 18:09

Grantland Chew


The only complete solution is to check for SDK version.

if (Build.VERSION.SDK_INT < 18) {    webView.clearView(); } else {    webView.loadUrl("about:blank"); } 
like image 43
sandalone Avatar answered Sep 27 '22 18:09

sandalone