Get last loaded web browser url without using webView.goBack () in Android

I want to write down the history url or the last loaded url without manually storing the history urls. Is it possible?

+8
android browser-history webkit android-webview
source share
2 answers

Found the answer in the docs ...

WebBackForwardList mWebBackForwardList = mWebView.copyBackForwardList(); String historyUrl = mWebBackForwardList.getItemAtIndex(mWebBackForwardList.getCurrentIndex()-1).getUrl(); 
+30
source share

You can keep a stack (list) of URLs visited by adding them to onPageFinished () and deleting them when the user backs up.

i.e.

 private List<String> urls = new ArrayList<String>(); @Override public final void onPageFinished(final WebView view,final String url) { urls.add(0, url); super.onPageFinished(view, url); } 

and then lock the return key in activity:

 @Override public boolean onKeyDown(int keyCode, KeyEvent event) { switch (keyCode) { case KeyEvent.KEYCODE_BACK: if (urls.size() == 1) { finish(); return true; } else if (urls.size() > 1) { urls.remove(0); // load up the previous url loadUrl(urls.get(0)); return true; } else return false; default: return super.onKeyDown(keyCode, event); } } 
0
source share

All Articles