How To Get Behaviour Of Super.onbackpressed() Within Overrided Onbackpressed()
Solution 1:
put finish() to finish your activity in onclick of positive button ...
builder.setPositiveButton("OK", newDialogInterface.OnClickListener() {
@OverridepublicvoidonClick(DialogInterface dialog, int which) {
//super.onBackPressed();
finish();
}
});
Solution 2:
To call the exact same super method:
YourActivity.super.onBackPressed();
Solution 3:
this.finish
is not allowed in my activity and this.finalize
is creapy code.
Android studioprojects wants it to be surrounded with try
/catch
.
As super.onBackPressed()
cannot be called in the onClick
method of the dialog interface, it is easier to call some private method instead of that.
Let that private method call super.onBackPressed()
and the result is the same as pressing the back button.
Solution 4:
If the user has pressed the back button, you can be pretty sure that they want to exit. However, with that said:
you should implement an OnClickListener
for your cancel button as well, with the following code:
publicvoidonClick(DialogInterface dialog, int which){
dialog.dismiss();
}
Solution 5:
try this:
boolean exit=false;
...
@Override
public void onBackPressed() {
if (exit){
exit=false;
super.onBackPressed();
}else {
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
exit = true;
onBackPressed();
break;
case DialogInterface.BUTTON_NEGATIVE:
break;
}
};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure").setPositiveButton("yes", dialogClickListener).setNegativeButton(getResources().getString("no"), dialogClickListener).show();
}
}
With this code in the main activity that contains the fragments, if user presses back on fragment,the app shows the dialog and if user presses 'yes' button, it will come back to call to onBackPressed, but now, it goes back to the last fragment.
Post a Comment for "How To Get Behaviour Of Super.onbackpressed() Within Overrided Onbackpressed()"