user3439226
user3439226

Reputation: 21

How to navigate webpage history in webview

I have webview included in my app. I want to navigate webpage history, but I don`t know how to do that. Please help me. My Main.java below:

package com.abc.test;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class Main extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    WebView wv = (WebView)findViewById(R.id.webview);
    WebSettings webSettings = wv.getSettings();
    webSettings.setJavaScriptEnabled(true);
    wv.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    });
    wv.loadUrl("http://www.google.com");

    }
}

Upvotes: 2

Views: 2606

Answers (1)

anuj
anuj

Reputation: 1060

Here is the code

public void goBackInWebView(){
    WebBackForwardList history = webView.copyBackForwardList();
    int index = -1;
    String url = null;

    while (webView.canGoBackOrForward(index)) {
          if (!history.getItemAtIndex(history.getCurrentIndex() + index).getUrl().equals("about:blank")) {
             webView.goBackOrForward(index);
             url = history.getItemAtIndex(-index).getUrl();
             Log.e("tag","first non empty" + url);
             break;
           }
           index --;

    }
   // no history found that is not empty
   if (url == null) {
      finish();
   }
}

Upvotes: 2

Related Questions