webView.canGoBack() not working when using loadDataWithBaseURL()
Solution 1
So I actually decided to implement my own "back" functionality, not sure if this will work for everybody´s intentions, but it worked for me.
First,
Create an ArrayList
that will store the urls you visit in chronological order.
private ArrayList<String> searchHistory;
from onCreate()
initialize it:
searchHistory = new ArrayList<String>();
Now, each time you load a url, simply add the url string to the history:
String mUrl = "www.example.com";
searchHistory.add(mUrl);
mWebView.loadDataWithBaseURL(mUrl,...);
And, when user presses back, handle that by removing the eldest entry of the array, and then loading the last one remaining after deletion:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && searchHistoryPos > 0) {
Constants.LogMessage("Handling back keyevent");
//remove eldest entry
searchHistory.remove(mUrl);
//make the url-to-load be the latest entry after deletion
mUrl = searchHistory.get(searchHistory.size);
//load the new url
mWebView.loadDataWithBaseURL(mUrl...);
}
}
Of course, when calling loadDataWithBaseUrl, I am also passing the pre-loaded HTML data.
Hope that helps!
Solution 2
another simple solution is to save the first url and than check if is equal to the current url
field :
String mFirstUrlLoaded = "";
than :
mFirstUrlLoaded = "your first url.....";
than :
@Override
public void onBackPressed() {
if (!mFirstUrlLoaded.equals(mWebView.getUrl())) {
mWebView.goBack();
}
Solution 3
put first link on history field
view.loadDataWithBaseURL(mUrl, mHtmlData, SearchUtils.MIME_TYPE, SearchUtils.CHARSET, mUrl);
Related videos on Youtube
daniel_c05
Updated on June 04, 2022Comments
-
daniel_c05 almost 2 years
I have a
WebView
in my application that displays HTML data downloaded from a webpage. Why do I load the HTML rather than the URL alone? Well, in order for me to allow offline search, I download the HTML data and store it on an SQL database.Everything works well, and subsequent calls to
loadDataWithBaseUrl()
work well, and keep on loading correctly on myWebView
, but whenever the user presses the back key, the user is just taken back to the last activity on the stack, rather than going back.I tried using the following code:
// Check if the key event was the Back button and if there's history if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) { myWebView.goBack(); return true; }
But then I tried forcing the
WebView
to go back, without checking if it could or not, via:// Check if the key event was the Back button and if there's history if ((keyCode == KeyEvent.KEYCODE_BACK)) { myWebView.goBack(); return true; }
but nothing happens, it just stays on the same place.
As an FYI, I've tried passing
null
and an actual URL when callingloadData()
, here's how I have it set right now:view.loadDataWithBaseURL(mUrl, mHtmlData, SearchUtils.MIME_TYPE, SearchUtils.CHARSET, "");
I read this answer but I am hoping for better luck.
-
Giru Bhai over 9 yearsdid you also save html data with urls?