在FragmentActivity重写onKeyDown方法:
@Override public boolean onKeyDown(int keyCode, KeyEvent event) { // TODO Auto-generated method stub if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) { //if no more history in stack if (this.getSupportFragmentManager().getBackStackEntryCount() == 0) { new AlertDialog.Builder(this).setMessage("退出程序?").setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub } }) .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { finish(); } }) .create().show(); return true; } } return super.onKeyDown(keyCode, event); }