gpt4 book ai didi

android - 按下后退按钮

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:33:12 24 4
gpt4 key购买 nike

我有 5 个不同屏幕的多个 Activity 。如果在第一个屏幕和其他屏幕中按下后退键,我应该如何处理这种情况?? 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);
}

最佳答案

像这样覆盖onBackPressed()...

@Override
public void onBackPressed()
{
finish();
}

关于android - 按下后退按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13387778/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com