title: Go back to Previous Page in Web View
body: <p>I have a webview fragment and I want to use the built in Android back button to go back to the previous page.</p>
  
  <pre><code>package kyfb.android.kyfb.com.kyfb;
  
  import android.app.Fragment;
  import android.content.Context;
  import android.content.Intent;
  import android.content.SharedPreferences;
  import android.graphics.Bitmap;
  import android.net.Uri;
  import android.os.Bundle;
  import android.view.KeyEvent;
  import android.view.LayoutInflater;
  import android.view.Menu;
  import android.view.MenuInflater;
  import android.view.MenuItem;
  import android.view.View;
  import android.view.ViewGroup;
  import android.webkit.WebChromeClient;
  import android.webkit.WebSettings;
  import android.webkit.WebView;
  import android.webkit.WebViewClient;
  import android.widget.ProgressBar;
  import android.widget.Toast;
  
  /**
   * Created by Adam Rayborn on 8/25/14.
   */
  public class MyKYFB extends Fragment {
  
      public WebView web;
      private ProgressBar progressBar;
  
      private String url;
  
      private static final String SP_LAST_WEBVIEW_URL = "com.app.kyfb.LASTWEBVIEWURL";
  
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
          super.onCreateView(inflater, container, savedInstanceState);
          View rootView = inflater.inflate(R.layout.fragment_my_kyfb, null);
  
          url = "https://my.kyfb.com";
  
          web = (WebView)rootView.findViewById(R.id.kyfb_web);
          progressBar = (ProgressBar)rootView.findViewById(R.id.prgPageLoading);
  
          web.getSettings().setDomStorageEnabled(true);
          web.getSettings().setJavaScriptEnabled(true);
          web.getSettings().setSupportZoom(true);
          web.getSettings().setBuiltInZoomControls(true);
          web.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
          web.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
          web.getSettings().setDefaultZoom(WebSettings.ZoomDensity.FAR);
  
  
          SharedPreferences sharedPrefs = getActivity().getPreferences(Context.MODE_PRIVATE);
          String savedUrl = sharedPrefs.getString("URL", null);
          if (savedUrl != null) {
              web.loadUrl(savedUrl);
          }
          else {
              web.loadUrl(url);
          }
  
          web.setWebChromeClient(new WebChromeClient() {
              public void onProgressChanged(WebView webView, int progress) {
                  getActivity().setProgress(progress * 100);  
                  progressBar.setProgress(progress);
              }
          });
  
          // this.getView().setFocusableInTouchMode(true);
          rootView.setFocusableInTouchMode(true);
  
          web.setWebViewClient(new WebViewClient() {
              @Override
              public void onPageStarted(WebView view, String url, Bitmap favicon) {
                  super.onPageStarted(web, url, favicon);
                  progressBar.setVisibility(View.VISIBLE);
              }
  
              @Override
              public void onPageFinished(WebView view, String url) {
                  super.onPageFinished(web, url);
                  progressBar.setProgress(0);
                  progressBar.setVisibility(View.GONE);
  
                  SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
                  SharedPreferences.Editor editor = sharedPref.edit();
                  editor.putString("URL", web.getUrl());
                  editor.apply();
              }
  
              public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                  Toast.makeText(getActivity(), description, Toast.LENGTH_SHORT).show();
              }
  
  
  
              public boolean shouldOverrideUrlLoading(WebView view, String url)
              {
                  if(url.endsWith(".mp4") || url.endsWith(".3gp") || url.endsWith(".avi") || url.endsWith(".flv") || url.endsWith(".pdf")){
                      Intent i = new Intent(Intent.ACTION_VIEW);
                      i.setData(Uri.parse(url));
                      startActivity(i); //warning no error handling will cause force close if no media player on phone.
                      return true;
                  }
  
                  view.loadUrl(url);
                  return true;
              }
          });
  
          this.getActivity().onBackPressed();
  
          return rootView;
      }
  }
  </code></pre>
  
  <p>In my main activity where this fragment is loaded, I have overridden onBackPressed()</p>
  
  <pre><code>@Override
      public void onBackPressed() {
          Fragment webview = getFragmentManager().findFragmentByTag("webview");
          if (webview instanceof MyKYFB) {
              boolean goback = ((MyKYFB) webview).web.canGoBack();
              if (!goback) {
                  super.onBackPressed();
              } else {
                  ((MyKYFB) webview).web.goBack();
              }
          }
      }
  </code></pre>
  
  <p>By doing this and calling this.getActivity.onBackPressed(); in the fragment, the back button doesn't do anything, but it doesn't go to the previous page if there is one.</p>