dro*_*der 8 android android-alertdialog
我想在我的应用程序中完成我的主菜单.我认为在OnBackPressed方法中添加AlertDialog是一个简单的好方法.但由于某种原因,我遇到了各种各样的错误.
我在OnBackPressed中创建了AlertDialog并显示它,但当我按下后退按钮时,应用程序才关闭,我得到错误,说窗口正在泄漏.
知道如何解决这个问题吗?我搜索了大约30分钟,找不到其他人有这个问题.
K_A*_*nas 37
尽量不要调用super.OnBackPressed(),这段代码可以帮助:
@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();
}
Run Code Online (Sandbox Code Playgroud)
小智 6
如果要调用super.onBackPressed(),请使用以下代码:
@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) {
//finish();
MyActivity.this.onSuperBackPressed();
//super.onBackPressed();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
/*
if (handleCancel()){
super.onBackPressed();
}
*/
}
public void onSuperBackPressed(){
super.onBackPressed();
}
Run Code Online (Sandbox Code Playgroud)
我刚刚在MyActivity中添加了onSuperBackPressed带有super-method 的新公共方法。
感谢K_Anas提供了出色的想法。
我在OnBackPressed中创建了AlertDialog并显示它,但是当我按下“后退”按钮时,应用程序仅关闭,并且我收到错误消息,指出窗口正在泄漏。
如果在您的内部OnBackPressed调用,super.OnBackPressed()则应用程序将完成,因为这就是基本实现OnBackPressed。不要调用该super方法,该应用程序也不会关闭。
| 归档时间: |
|
| 查看次数: |
14778 次 |
| 最近记录: |