1

Actually my issue is when i press the back button it shows a dialog and also at the same time the application finished without any operation on key? Can any one solve this?

@Override
public void onBackPressed() {
    super.onBackPressed();
    AlertDialog.Builder ab = new AlertDialog.Builder(this);
    ab.setMessage("Really want to exit");
    ab.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            finish();
        }
    });
    ab.setNegativeButton("No", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Main.super.onBackPressed();
        }
    });
    ab.create();
    ab.show();
}
0

4 Answers 4

3

remove super.onBackPressed(); from method, keep it in onClick() only

@Override
public void onBackPressed() {
    super.onBackPressed(); //remove it

}

seems you are performing same operation in both buttons, you should remove Main.super.onBackPressed() from negativeButton

ab.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        super.onBackPressed();
    }
});
ab.setNegativeButton("No", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        ab.dismiss();
    }
});
1

Don't call super method of onBackPressed.

@Override
public void onBackPressed() {

    AlertDialog.Builder ab = new AlertDialog.Builder(this);
    ab.setMessage("Really want to exit");
    ab.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            finish();
        }
    });
    ab.setNegativeButton("No", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {

        }
    });
    ab.create();
    ab.show();
}
2
  • if i press no also the app closed how do restrict in to that activity if i press no
    – A.asha
    Commented Jul 11, 2016 at 11:21
  • @A.asha check my updated answer. remove this Main.super.onBackPressed(); Commented Jul 11, 2016 at 11:26
0
try this
  @Override
    public void onBackPressed() {

        AlertDialog.Builder ab = new AlertDialog.Builder(this);
        ab.setMessage("Really want to exit");
        ab.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
               finish();
            }
        });
        ab.setNegativeButton("No", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Main.super.onBackPressed();
            }
        });
        ab.create();
        ab.show();
    }
0

You need to Remove, super.onBackPressed(); from onBackPressed(). then your function should as,

@Override
public void onBackPressed() {

AlertDialog.Builder ab = new AlertDialog.Builder(this);
ab.setMessage("Really want to exit");
ab.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        finish();
    }
});
ab.setNegativeButton("No", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        Main.super.onBackPressed();
    }
});
ab.create();
ab.show();
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.