title: onKeyDown not working in my code for fragment
body: <p>I try to make a alert dialog box when anyone press back button, incase of fragment. I try it several way but I am not able to do it. Is there any way to do it inside fragment. </p>
  
  <p><a href="https://i.stack.imgur.com/m9StC.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/m9StC.png" alt="enter image description here"></a></p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="false" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>package com.topicsfeedback.www.mypackage.activity;
  
  /**
   * Created by hasib on 2/22/2016.
   */
  import android.app.Activity;
  import android.app.AlertDialog;
  import android.content.DialogInterface;
  import android.content.Intent;
  import android.os.Bundle;
  import android.support.v4.app.Fragment;
  import android.view.KeyEvent;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  
  import com.topicsfeedback.www.mypackage.R;
  
  import java.net.Inet4Address;
  
  
  public class MessagesFragment extends Fragment {
  
      public MessagesFragment() {
          // Required empty public constructor
      }
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState) {
          View vv = inflater.inflate(R.layout.fragment_messages, container, false);
  
          return vv;
      }
  
  
      @Override
      public void onAttach(Activity activity) {
          super.onAttach(activity);
      }
    
    // Back button press start
  
      @Override
      public boolean onKeyDown(int keyCode, KeyEvent event) {
          if (keyCode == KeyEvent.KEYCODE_BACK) {
              new AlertDialog.Builder(getActivity())
                      .setIcon(android.R.drawable.ic_dialog_alert)
                      .setTitle("Exit This Apps")
                      .setMessage("Do You want to exit?")
                      .setPositiveButton("YES", new DialogInterface.OnClickListener() {
                          @Override
                          public void onClick(DialogInterface dialog, int which) {
                              Intent myinnn = new Intent(getActivity(), HomeFragment.class);
                              startActivity(myinnn);
                          }
                      })
                      .setNegativeButton("NO", null)
  
                      .show();
              return true;
          }
    
   
          else {
              return super.onKeyDown(keyCode, event);  //Here onKeyDown can't resolve 
          }
  
  
      }
  
  //Backbutton press end
      
      @Override
      public void onDetach() {
          super.onDetach();
  
      }
  }</code></pre>
  </div>
  </div>
  </p>