title: Using onKeyDown or onBackKey pressed in android
body: <p><strong>What I am doing</strong>::</p>
  
  <p>I am trying to check the condition whether to quit the application on back key pressed</p>
  
  <p><strong>What is happening</strong> ::</p>
  
  <p>I am able to launch the dialog but immediately the activity is removed before I could make any selection in dialog </p>
  
  <p>How can I resolve this?</p>
  
  <hr>
  
  <pre><code>public boolean onKeyDown(int keyCode, KeyEvent event)
  {
      if (keyCode == KeyEvent.KEYCODE_BACK)
      {
             /*FragmentManager.BackStackEntry backEntry=getSupportFragmentManager().getBackStackEntryAt(getFragmentManager().getBackStackEntryCount()-1);
              String str=backEntry.getName();
              Log.d("Back-Name", str);*/
  
  
          if(getSupportFragmentManager().findFragmentByTag("HomeFragment")!=null ){
              Log.d("My-Log-Msg","$-before-showDialog-$");
              showGpsDialog();
              Log.d("My-Log-Msg","$-after-showDialog-$");
          }else
          {
              /*Intent intent = getIntent();
              finish();
              startActivity(intent);*/
              ft.replace(R.id.content_frame, fragment1,"HomeFragment");
          }
      }
      return super.onKeyDown(keyCode, event);
  }
  
  
  public void showGpsDialog(){
      //GPS-Dialog
      WillYouQuitAppDialog quitAlert;
      try {
          quitAlert = new WillYouQuitAppDialog();
          quitAlert.show(getSupportFragmentManager(), "GpsAlert_Tag");
      } catch (Exception e) {
          // TODO Auto-generated catch block
          Log.d("My-Log-Msg",e.toString());
          e.printStackTrace();
      }
  }
  </code></pre>