Android的提示一个AlertDialog onBack pressed [英] Android Prompting an AlertDialog onBackPressed
问题描述
我试图完成了我的主菜单我的应用程序。我认为这将是一个简单的很好的接触在OnBack pressed方法添加一个AlertDialog。但是由于某种原因,我收到各种错误。
I am trying to finish up my main menu in my application. I thought it would be a simple nice touch to add an AlertDialog in the OnBackPressed method. However for some reason I am getting all kinds of errors.
我创建的AlertDialog在pssed的OnBack $ P $,并显示它,但应用程序只是关闭时我preSS后退按钮和我得到的错误说,窗口泄漏。
I created the AlertDialog in the OnBackPressed and show it but the app just closes when I press the back button and I get errors saying that the window is leaking.
不知道如何解决这一问题?我搜索了大约30分钟,但没有找到任何人有这个问题。
Any idea how to fix this? I searched for about 30 minutes and couldn't find anyone else with this problem.
推荐答案
尽量不叫 super.OnBack pressed(),这code建议立即进行删除帮助:
Try to not call super.OnBackPressed(), this code shoul help:
@Override
public void onBackPressed() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
MyActivity.this.finish();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
这篇关于Android的提示一个AlertDialog onBack pressed的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!