How to disable the back button when the alert box is on the screen
I used onKeyDown function in the activity.....but when back button button is clicked it first cancels the dialog box and goes to our activity...I want both either both activity and dialog box closed when clicking the back button or disable the back button when the dialog box is shown...
can any one suggest any solutions for this....
Thanks in advance,
Update
Hello Thanks for your answer.
The progressDialog
with .开发者_如何转开发setCancelable(false);
is working fine.
But here I want different thing. When the progress dialog is running then i will press the BACK key and i want to show an alert dialog so that the user can notify that the progress is running.
Is there any solution about it?
Please help me.
Thanks in advance.
Did you try setting its setCancelable()
property to false
Something like this
progressDialog.setCancelable(false);
May be this will help in your case:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
// Show your Alert Box here
}
return false;
}
I just want to improve @MoJo answer
alertDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
@Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
//Your handler
return false;
}
});
Personally a cleaner solution was to finish the LoginActivity before starting the intent instead of setting Flags in the bundle of the new activity or overriding any methods. Try something like this below where Login is your login activity and Home is the first activity after successfully login in the user.
finish();
Intent intent = new Intent(Login.this, Home.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
"I want both either both activity and dialog box closed"
dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
@Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
finish();
return true;
}
});
"or disable the back button when the dialog box is shown" This can block going back from dialog. When creating it dynamically, add:
dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
@Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
return true;
}
});
Returning true is preventing this dialog from closing, and it cannot be closed by clicking back. Also I suggest adding:
dialog.setCanceledOnTouchOutside(false);
What will prevent user from dismissing dialog by clicking outside of it.
精彩评论