再按一次退出

press back again to exit

我有一个应用程序,我想在用户触摸后退按钮时执行此操作 Toast 显示 "press back again to exit" 但我对这部分有疑问。在此代码上,当触摸后退按钮应用程序完全完成而没有吐司并再次触摸后退按钮时。请帮助我。

   public void onStop(){
        super.onStop();
        if(key == 1){
            key =0;
            finish();
        }else{
            Toast.makeText(getApplicationContext(), "press back Button again to exit", Toast.LENGTH_SHORT).show();
        key++;
        }
            return;
        }

您必须在 onBackPressed() 方法中编写代码。试试这个代码:

@Override 
public void onBackPressed() { 
    if(key == 1){
            key =0;
            finish();
        }else{
            Toast.makeText(getApplicationContext(), "press back Button again to exit", Toast.LENGTH_SHORT).show();
        key++;
        }
}

我建议您遵循此 post:Clicking the back button twice to exit an activity

@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);
} 

你可以通过这个link Clicking the back button twice to exit an activity

同样的问题