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.
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.
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...
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.
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()"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With