再次按回退出

时间:2015-07-02 18:04:54

标签: android toast

我有一个应用程序,我想这样做当用户触摸按钮一个Toast节目“再次按回退出”但我有这个部分的问题。在这个代码上,当触摸按钮应用程序完全完成没有吐司和再次触摸按钮。请帮帮我。

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

2 个答案:

答案 0 :(得分:4)

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

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

同样的问题

答案 1 :(得分:2)

您必须在@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++; } } 方法中编写代码。试试这段代码:

{{1}}

我建议您关注此帖:Clicking the back button twice to exit an activity