开发者

Get last loaded url of webview without doing a webView.goBack() in Android

I want to log the history url or last loaded url withou开发者_如何学编程t manually storing the history urls. Is that possible?


Found the answer in the docs ...

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


You could maintain a stack(list) of URL's visited by adding them in onPageFinished() and removing 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 trap the back key in the 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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜