title: How to use onKeyDown Method in Android Fragment for webView
body: <p>I am making an Android application which have some items in Navigation Drawer and every item contain link for different pages of a website, Now i want to implement onKeyDown method or onBackPrssed method for webView, How can i achieve this my Navigation Drawer using Fragments.</p>
  
  <p>HomeFragment.java </p>
  
  <pre><code>public class HomeFragment extends Fragment {
      WebView webView;
      ProgressDialog progressDialog;
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState) {
          View view=inflater.inflate(R.layout.home_fragment, container, false);
          webView = (WebView) view.findViewById(R.id.webView);
          homePage();
          return view;
      }
  
      public void homePage()
      {
  
          webView.getSettings().setJavaScriptEnabled(true);
          webView.getSettings().setBuiltInZoomControls(true);
          webView.getSettings().setDisplayZoomControls(false);
          webView.loadUrl("http://google.com/");
          webView.setWebViewClient(new WebViewClient(){
              @Override
              public boolean shouldOverrideUrlLoading(WebView view, String url) {
                  view.loadUrl(url);
                  return false;
              }
  
              @Override
              public void onPageStarted(WebView view, String url, Bitmap favicon) {
                  super.onPageStarted(view, url, favicon);
                  progressDialog = new ProgressDialog(getActivity());
                  progressDialog.setTitle("Loading");
                  progressDialog.setMessage("Your page is loading...");
                  progressDialog.setCancelable(false);
                  progressDialog.setCanceledOnTouchOutside(false);
                  progressDialog.show();
              }
  
              @Override
              public void onPageFinished(WebView view, String url) {
                  super.onPageFinished(view, url);
                  progressDialog.dismiss();
              }
           });
          webView.setOnKeyListener(new View.OnKeyListener() {
              @Override
              public boolean onKey(View view, int keyCode, KeyEvent keyEvent) {
                  if((keyCode == KeyEvent.KEYCODE_BACK) &amp;&amp; webView.canGoBack())
                  {
                      webView.goBack();
                      return true;
                  }
                  return false;
              }
          });
      }
  
  
  }
  </code></pre>
  
  <p>HomeFragment is open after Navigation Drawer item clicked and it will open a webpage. setOnClickListener not working fine it redirect me to first page not previous page.</p>