简体   繁体   English

在退出应用程序之前按两次“返回”按钮

[英]Back button press twice before quitting app

How can I configure the back button to be pressed twice before the app exits? 如何配置后退按钮在应用程序退出之前被按下两次? I want to trigger 我想触发

@Override
public void onBackPressed() {
    //custom actions
    //display toast "press again to quit app"
    super.onBackPressed();
}

Try this: 尝试这个:

private boolean doubleBackToExitPressedOnce = false;

@Override
protected void onResume() {
    super.onResume();
    // .... other stuff in my onResume ....
    this.doubleBackToExitPressedOnce = false;
}

@Override
public void onBackPressed() {
    if (doubleBackToExitPressedOnce) {
        super.onBackPressed();
        return;
    }
    this.doubleBackToExitPressedOnce = true;
    Toast.makeText(this, "Press twice to exit", Toast.LENGTH_SHORT).show();

}

This snippet handle also the reset state when the activityis resumed 当活动恢复时,此代码段句柄还将重置状态

I see this question is a bit old but I though this might help some people looking for an alternative to the answers already given. 我看到这个问题有点老了,但我虽然可以帮助某些人寻找已经给出的答案的替代方案。

This is how I handle backing out of my applications. 这就是我处理退出应用程序的方式。 If someone has a better -- or a Google suggested -- method of accomplishing this I'd like to know. 如果有人有更好的方法(或Google推荐的方法),我想知道。

Edit -- Forgot to mention this is for Android 2.0 and up. 编辑-忘记提及这是针对Android 2.0及更高版本。 For previous versions override onKeyDown(int keyCode, KeyEvent event) and check for keyCode == KeyEvent.KEYCODE_BACK . 对于onKeyDown(int keyCode, KeyEvent event)版本,请重写onKeyDown(int keyCode, KeyEvent event)并检查keyCode == KeyEvent.KEYCODE_BACK Here is a good link to check out. 是一个结帐的好链接。

private boolean mIsBackEligible = false;

@Override
public void onBackPressed() {

    if (mIsBackEligible) {

        super.onBackPressed();

    } else {

        mIsBackEligible = true;
        new Runnable() {
            // Spin up new runnable to reset the mIsBackEnabled var after 3 seconds
            @Override
            public void run() {
                CountDownTimer cdt = new CountDownTimer(3000, 3000) {
                    @Override
                    public void onTick(long millisUntilFinished) { 
                        // I don't want to do anything onTick
                    }

                    @Override
                    public void onFinish() {
                        mIsBackEligible = false;
                    }
                }.start();
            }
        }.run(); // End Runnable()

        Toast.makeText(this.getApplicationContext(),
                "Press back once more to exit", Toast.LENGTH_SHORT).show();

    }

}

You could do what you're asking with a global integer and just count it, if > 2, quit. 您可以使用全局整数来完成您要问的事情,如果> 2,则将其计数。

But you could take a better (IMO) approach, where you question the user if they would like to quit or not: 但是您可以采用更好的(IMO)方法,在该方法中,询问用户是否要退出:

private void questionQuit(){
    final CharSequence[] items = {"Yes, quit now", "No, cancel and go back"};

    builder = new AlertDialog.Builder(mContext);
    builder.setCancelable(false);
    builder.setTitle("Are you sure you want to quit?");
    builder.setItems(items, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int item) {
            switch(item){
            case 0:
                quit();
                break;
            case 1:
            default:
                break;
            }
        }
    }).show();
    AlertDialog alert = builder.create();

}
@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_DOWN) {
            switch (keyCode) {
            case KeyEvent.KEYCODE_BACK :
                int i = 0 ;
                   if(i == 1 )
                      {
                       finish();
                      }
                     i++;

                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM