我有多个活动(activities),包含5个不同的屏幕。如果在第一屏或其他任何屏幕中按下返回键,我应该如何处理这种情况?finish()和System.exit()都无法正常工作。如果按下后退按钮,我应该怎么退出我的应用程序呢?以下代码无法解决问题,请建议一种解决方案,以便退出一个拥有多个屏幕的应用程序。
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK)
{
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
this);
new AlertDialog.Builder(this) .setMessage("Are you sure you want to exit?")
.setCancelable(true)
.setPositiveButton("Yes", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface hi, int dd)
{
Intent exitIntent = new Intent(Mapper.this,SplashActivity.class);
exitIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP|Intent.FLAG_ACTIVITY_CLEAR_TOP);
SplashActivity.mHandler.sendEmptyMessage(0);
startActivity(exitIntent);
}
}
)
.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface hi, int dd)
{
}
}
);
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
//AlertDialog alert1 = alt_bld.create();
//alert1.setTitle("EXIT");
//alert1.show();
return true;
}
return super.onKeyDown(keyCode, event);
}
And in Start.Class:
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_HOME)
{
mHandler.removeCallbacksAndMessages(null);
SplashActivity.this.finish();
}
return super.onKeyDown(keyCode, event);
}