android - 为什么此警报对话框未在 backPressed 上显示?
问题描述
这是我的 onBackPressed() :
@Override
public void onBackPressed() {
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(MainScreen.this);
String checkInVal = preferences.getString("checkIn", "");
if (!PRE_USER_ACTIVE.equalsIgnoreCase("1")) {
super.onBackPressed();
finish();
} else if (MainScreen.this.getSupportFragmentManager().findFragmentByTag("VOLUNTEERDETAILS") != null && checkInVal.equals("yes")) {
popFragment();
} else {
if (Functions.iNumberOfLoadedFragments == 0) {
String exitLog = "Exit Application", ok = "OK", cancel = "CANCEL";
String sureLog = "Are you sure you want to exit " + MainScreen.this.getResources().getString(R.string.app_name);
if (EasyPreference.with(MainScreen.this, PRE_KEY)
.getString(PRE_USER_LANGUAGE, PRE_LANGUAGE_ENGLISH).equalsIgnoreCase(PRE_LANGUAGE_ASSEMESS)) {
exitLog = "প্ৰস্থান কৰক অ্যাপ্লিকেশন";
sureLog = "আপুনি Our SchoolZoneৰ পৰা প্ৰস্থান কৰিবলৈ নিশ্চিতনে";
ok = "স্বীকাৰ কৰক";
cancel = "বাতিল কৰক";
} else if (EasyPreference.with(MainScreen.this, PRE_KEY)
.getString(PRE_USER_LANGUAGE, PRE_LANGUAGE_ENGLISH).equalsIgnoreCase(PRE_LANGUAGE_THI)) {
exitLog = "ออกจากแอพพลิเคชัน";
sureLog = "คุณแน่ใจหรือว่าต้องการออกจาก Our SchoolZone";
ok = "ตกลง";
cancel = "ยกเลิก";
}
new LovelyStandardDialog(MainScreen.this)
.setTopColor(Functions.getBackColor())
.setIcon(R.drawable.ic_info_outline_white_36dp)
.setTitle(exitLog)
.setMessage(sureLog)
.setPositiveButton(ok, new View.OnClickListener() {
@Override
public void onClick(View v) {
MainScreen.super.onBackPressed();
}
})
.setNegativeButton(cancel, new View.OnClickListener() {
@Override
public void onClick(View v) {
}
})
.show();
} else if (Functions.iNumberOfLoadedFragments > 0) {
Functions.iNumberOfLoadedFragments--;
super.onBackPressed();
}
}
}
ALertDialog 需要显示在从此 Activity 按下的后退按钮中,而不是应用程序只是退出到启动器。
有什么可能的问题,请回复您的宝贵建议。谢谢
解决方案
使用以下代码: AlertDialog for exit from app
@Override
public void onBackPressed() {
new AlertDialog.Builder(this)
.setMessage("Are you sure you want to EXIT ?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
moveTaskToBack(true);
finish();
}
})
.setNegativeButton("No", null)
.show();
}
如果觉得有用,请点赞,以便其他人可以得到答案
推荐阅读
- python - 使用 python 字典和 agraph 在 Graphviz 中标记节点
- java - How to compare single element to any element in a collection or list
- masstransit - 有没有办法在设置消费者时定义 Azure 服务总线规则/过滤器?
- c# - Add foreign key inside my C# code for Sqlite
- pdfbox - 如何将带有fillorder 2的tiff图像转换为pdf而不使用itext,最好是pdfbox
- bilby - 如何注入零噪声信号紧凑二进制合并信号
- javascript - 为什么在使用 google oauth api 时我的属性名称很奇怪?
- mysql - Sql 在另一列的同一张表中对具有相同值的项目进行分组
- sql - 从一个表中标记连接表中存在值的行?
- project - 为什么我不能在 IBM Watson Studio 中创建项目?