Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
547 views
in Technique[技术] by (71.8m points)

android - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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()"


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.8k users

...