如何显示对话框以确认用户希望退出Android活动?

我一直试图performance出“你想退出吗?” 用户尝试退出活动时的对话框types。

但是我找不到合适的API钩子。 Activity.onUserLeaveHint()最初看起来很有前途,但是我找不到一种方法来停止Activity的完成。

在Android 2.0+中,这看起来像:

 @Override public void onBackPressed() { new AlertDialog.Builder(this) .setIcon(android.R.drawable.ic_dialog_alert) .setTitle("Closing Activity") .setMessage("Are you sure you want to close this activity?") .setPositiveButton("Yes", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { finish(); } }) .setNegativeButton("No", null) .show(); } 

在以前的版本中,它看起来像:

 @Override public boolean onKeyDown(int keyCode, KeyEvent event) { //Handle the back button if(keyCode == KeyEvent.KEYCODE_BACK) { //Ask the user if they want to quit new AlertDialog.Builder(this) .setIcon(android.R.drawable.ic_dialog_alert) .setTitle(R.string.quit) .setMessage(R.string.really_quit) .setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //Stop the activity YourClass.this.finish(); } }) .setNegativeButton(R.string.no, null) .show(); return true; } else { return super.onKeyDown(keyCode, event); } } 
 @Override public void onBackPressed() { new AlertDialog.Builder(this) .setMessage("Are you sure you want to exit?") .setCancelable(false) .setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { ExampleActivity.this.finish(); } }) .setNegativeButton("No", null) .show(); } 

已经修改@ user919216代码..并使其与WebView兼容

 @Override public void onBackPressed() { if (webview.canGoBack()) { webview.goBack(); } else { 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(); } }) .setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.cancel(); } }); AlertDialog alert = builder.create(); alert.show(); } } 

我宁愿退出与后退button双击,而不是退出对话框。

在这个解决scheme中,当第一次回来的时候会显示一个敬酒,警告另一个后面的新闻会closures应用程序。 在这个例子中less于4秒。

 private Toast toast; private long lastBackPressTime = 0; @Override public void onBackPressed() { if (this.lastBackPressTime < System.currentTimeMillis() - 4000) { toast = Toast.makeText(this, "Press back again to close this app", 4000); toast.show(); this.lastBackPressTime = System.currentTimeMillis(); } else { if (toast != null) { toast.cancel(); } super.onBackPressed(); } } 

令牌从: http : //www.androiduipatterns.com/2011/03/back-button-behavior.html

如果您不确定“返回”的调用是否将退出应用程序,或者将用户转到另一个活动,则可以将上述答案包装在isTaskRoot()中。 如果您的主要活动可以多次添加到后台堆栈中,或者您正在操作回退堆栈历史logging,则会发生这种情况。

 if(isTaskRoot()) { 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) { YourActivity.super.onBackPressed; } }) .setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.cancel(); } }); AlertDialog alert = builder.create(); alert.show(); } else { super.onBackPressed(); } 

使用Lambda:

  new AlertDialog.Builder(this).setMessage(getString(R.string.exit_msg)) .setTitle(getString(R.string.info)) .setPositiveButton(getString(R.string.yes), (arg0, arg1) -> { moveTaskToBack(true); finish(); }) .setNegativeButton(getString(R.string.no), (arg0, arg1) -> { }) .show(); 

您还需要在您的gradle.build中设置级别的语言来支持java 8:

 compileOptions { targetCompatibility 1.8 sourceCompatibility 1.8 } 

我喜欢@GLee的方法,并使用下面的片段。

 @Override public void onBackPressed() { if(isTaskRoot()) { new ExitDialogFragment().show(getSupportFragmentManager(), null); } else { super.onBackPressed(); } } 

使用片段的对话框:

 public class ExitDialogFragment extends DialogFragment { @Override public Dialog onCreateDialog(Bundle savedInstanceState) { return new AlertDialog.Builder(getActivity()) .setTitle(R.string.exit_question) .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { getActivity().finish(); } }) .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { getDialog().cancel(); } }) .create(); } } 

先删除super.onBackPressed();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(); } 

这工作正常

 @Override public void onBackPressed() { if (mExitOnBackPress) finish(); else { Toast.makeText(this, R.string.msg_confirm_exit, Toast.LENGTH_SHORT).show(); mExitOnBackPress = true; new android.os.Handler().postDelayed(new Runnable() { @Override public void run() { mExitOnBackPress = false; } }, 2000); } } 

在中国,大多数应用程序将通过“点击两次”确认退出:

 boolean doubleBackToExitPressedOnce = false; @Override public void onBackPressed() { if (doubleBackToExitPressedOnce) { super.onBackPressed(); return; } this.doubleBackToExitPressedOnce = true; Toast.makeText(this, "Please click BACK again to exit", Toast.LENGTH_SHORT).show(); new Handler().postDelayed(new Runnable() { @Override public void run() { doubleBackToExitPressedOnce=false; } }, 2000); }