How to show a dialog box after pressing the back button How to show a dialog box after pressing the back button android android

How to show a dialog box after pressing the back button


 public boolean onKeyDown(int keyCode, KeyEvent event) {    if (keyCode == KeyEvent.KEYCODE_BACK) {        exitByBackKey();        //moveTaskToBack(false);        return true;    }    return super.onKeyDown(keyCode, event);}protected void exitByBackKey() {    AlertDialog alertbox = new AlertDialog.Builder(this)    .setMessage("Do you want to exit application?")    .setPositiveButton("Yes", new DialogInterface.OnClickListener() {        // do something when the button is clicked        public void onClick(DialogInterface arg0, int arg1) {            finish();            //close();        }    })    .setNegativeButton("No", new DialogInterface.OnClickListener() {        // do something when the button is clicked        public void onClick(DialogInterface arg0, int arg1) {                       }    })      .show();}


This is a simpler solution :

@Overridepublic void onBackPressed() {    AlertDialog.Builder builder = new AlertDialog.Builder(this);    builder.setTitle("Save Or Not");    builder.setMessage("Do you want to save this? ");    builder.setPositiveButton("Save", new DialogInterface.OnClickListener() {        public void onClick(DialogInterface dialog, int id) {            saveResult();            MyActivity.super.onBackPressed();        }    });    builder.setNegativeButton("Discard", new DialogInterface.OnClickListener() {        public void onClick(DialogInterface dialog, int id) {            MyActivity.super.onBackPressed();        }    });    builder.show();}


 @Overridepublic void onBackPressed() {// TODO Auto-generated method stub      AlertDialog.Builder builder=new AlertDialog.Builder(mContext);     // builder.setCancelable(false);      builder.setTitle("Rate Us if u like this");      builder.setMessage("Do you want to Exit?");      builder.setPositiveButton("yes",new DialogInterface.OnClickListener() {        @Override        public void onClick(DialogInterface dialog, int which) {            // TODO Auto-generated method stub            Toast.makeText(mContext, "Yes i wanna exit", Toast.LENGTH_LONG).show();            finish();        }    });      builder.setNegativeButton("No",new DialogInterface.OnClickListener() {        @Override        public void onClick(DialogInterface dialog, int which) {            // TODO Auto-generated method stub            Toast.makeText(mContext, "i wanna stay on this page", Toast.LENGTH_LONG).show();            dialog.cancel();        }    });      builder.setNeutralButton("Rate",new DialogInterface.OnClickListener() {        @Override        public void onClick(DialogInterface dialog, int which) {            // TODO Auto-generated method stub            final String appPackageName = getPackageName(); // getPackageName() from Context or Activity object            try {                startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + getPackageName())));            } catch (android.content.ActivityNotFoundException anfe) {                startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://play.google.com/store/apps/details?id=" + getPackageName())));            }        }    });      AlertDialog alert=builder.create();      alert.show();     //super.onBackPressed();       }