Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I clear previous WebView's content before loading the next WebView?

My scenario is initially I am loading a WebView and later on I am loading the same WebView with a different URL.

My problem is whenever I am loading the next URL I can see the previously loaded URL contents and then my currently loaded URL contents gets displayed.

I want to clear the contents whenever I am loading the different URL.

Say,

if (pos == 0) {     mweb.clearCache(true);     mweb.clearHistory();     mweb.loadUrl("http://stackoverflow.com/"); } else if (pos == 1) {     mweb.clearCache(true);     mweb.clearHistory();     mweb.loadUrl("http://android.stackexchange.com/"); } 

Both clearCache() and clearHistory() does not work for me.

like image 798
Jack Dsilva Avatar asked Jan 20 '12 09:01

Jack Dsilva


People also ask

How do you override a WebView?

If you want to override certain methods, you have to create a custom WebView class which extends WebView . Also, when you are inflating the WebView , make sure you are casting it to the correct type which is CustomWebView . CustomWebView webView = (CustomWebView) findViewById(R. id.

When the back button is pressed in WebView the app exit?

Bookmark this question. Show activity on this post. Instead of going back to the previous page, the app is closed when pressed the back button...

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.


1 Answers

use mweb.clearView(); before loading the new URL.

Edit:

clearView() was deprecated in API level 18. Use WebView.loadUrl("about:blank") to reliably reset the view state and release page resources (including any running JavaScript).

to solve the issue raised by @Liang:

To have a consistent back navigation, you can override "onPageFinished", and inside it check whether the url contains "about:blank" or not. if yes, use "webview.goBack()"

like image 130
a fair player Avatar answered Nov 10 '22 08:11

a fair player