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

18,222

Found the answer in the docs ...

WebBackForwardList mWebBackForwardList = mWebView.copyBackForwardList();
String historyUrl = mWebBackForwardList.getItemAtIndex(mWebBackForwardList.getCurrentIndex()-1).getUrl();
Share:
18,222
Arnab Chakraborty
Author by

Arnab Chakraborty

Began with C++, became a fan of C, went through Java out of curiosity and am now stuck with Android & ASP.NET. Have fallen in love with design principles and software engineering in general.

Updated on June 08, 2022

Comments

  • Arnab Chakraborty
    Arnab Chakraborty almost 2 years

    I want to log the history url or last loaded url without manually storing the history urls. Is that possible?

  • Torid
    Torid over 12 years
    onPageFinished tends to get called multiple times per page, particularly if there is redrawing (eg blinking cursors) or iframes. You could also shouldOverrideUrlLoading, which only gets called for a new URL, but that doesn't get called for iframes. So the best approach is to use a combination of use onPageFinished and shouldOverrideUrlLoading as described at stackoverflow.com/questions/3149216/…
  • tarrant
    tarrant over 12 years
    Yep, true. You'd have to see if the url was in the list already (at the zeroth position) and not add it in that case.
  • Arnab Chakraborty
    Arnab Chakraborty over 12 years
    I guess I didn't make myself clear, I don't want to manually save the urls. The webview does maintain a history stack, right? I just want to get the last loaded url.
  • Adil Malik
    Adil Malik over 10 years
    It works but there is a mistake. If you are on PageA then you go to PageB. Now when you press back button it takes you correctly back to pageA. But now if you press back button again, it will take you to PageB again because pageB was the previous page. So, it will loop between the two pages forever.
  • Arnab Chakraborty
    Arnab Chakraborty over 10 years
    In any case I wasn't using the history to navigate to pages, so it wouldnt be an issue in my case. I just needed it for logging purposes. Are you using the backForwardList to navigate? If so, why? Android browser would take care of that by default, wouldnt it?
  • Adil Malik
    Adil Malik over 10 years
    I had to implement a very simple browser. I used this instead: if(browser.canGoBack()){ browser.goBack() }else{ finish() } This worked fine for me.