Reputation: 53600
In my main activity, i call a dialog which is cancelable. I show this dialog when user lunches the app while he is not connected to Internet. therefore, I show this dialog to ask him connect to Internet.
This dialog doesn't have button and I want to close application when user clicks back button. In onKeyDown()
method, I'm closing the application (this.finish();
) but the problem is when dialog is displaying on screen when user clicks back button, this dialog disappear and my main activity is show.
It seems onKeyDown() just work in main activity not for dialog. How to close my app when dialog is display and user clicks back button?
Thanks
=====> Update
This is the code of my custom dialog:
private void initWarningDialog(){
dialogWarning = new Dialog(DRMActivity.this, R.style.customDialogStyle);
dialogWarning.setTitle("Warning!");
dialogWarning.setContentView(R.layout.dialogwarning);
dialogWarning.setCancelable(true);
}
Upvotes: 0
Views: 3976
Reputation: 128428
Only call finish() method:
dialog.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Finish activity
finish();
}
});
Update:
As you are having custom dialog, i would suggest you to set OnCancelListner, something like:
dialogWarning.setOnCancelListener(new OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
// TODO Auto-generated method stub
finish();
}
});
Update-2:
It is really annoying because how user can come to know if he press the back key then actual activity will close. Instead you should provide atleast "ok" button to let user click.
Upvotes: 5
Reputation: 2024
OnCancelListener
could be useful.
alert.setOnCancelListener(new OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
// TODO Auto-generated method stub
finish();
}
});
Upvotes: 2
Reputation: 6335
Set OnCancelListener
for dialog using setOnCancelListener
and finish activity in onCancel
method of OnCancelListener.
Upvotes: 2