这是我的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 相反,该应用仅退出启动器。
可能出现的问题,请回复您的宝贵建议。谢谢
答案 0 :(得分:0)
使用以下代码: AlertDialog,退出应用
@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();
}
upvote(如果发现有用),以便其他人可以得到答案