按两次以退出活动不起作用

时间:2015-09-22 08:00:34

标签: java android

我需要关闭并激活并仅在用户按下两次时启动另一个。我正在使用此代码

@Override
public void onBackPressed() {
    if (doubleBackToExitPressedOnce) {
        Log.e("Entering","Yes");
        DatabaseHandler db=new DatabaseHandler(AddBreakfastActivity.this);
        db.deleteTodaysUnsavedMenu(Integer.parseInt(newDay),Integer.parseInt(newIntMonth));
        Intent intent=new Intent(AddBreakfastActivity.this,ProvidersUpdateActivity.class);
        startActivity(intent);
        finish();
    }
    else {
        this.doubleBackToExitPressedOnce = true;
        Log.e("BOOLEANVALUE", String.valueOf(doubleBackToExitPressedOnce));
        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);
    }
}

一旦应用程序退出并显示Toast消息,则按下该按钮。 它不等第二次按下。我该如何解决这个问题? 谢谢。

修改

按下后退按钮时,发现它按预期工作。但是从

调用时显示上述问题
 public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if(id==android.R.id.home){
            onBackPressed();
        }
}

5 个答案:

答案 0 :(得分:1)

你也可以在keydown事件中执行此操作,因为下面的代码删除了反向压缩

@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub

        if(event.getAction()==KeyEvent.ACTION_DOWN && keyCode==KeyEvent.KEYCODE_BACK)
        {
            if (doubleBackToExitPressedOnce) {
                Log.e("Entering","Yes");
                DatabaseHandler db=new DatabaseHandler(AddBreakfastActivity.this);
                db.deleteTodaysUnsavedMenu(Integer.parseInt(newDay),Integer.parseInt(newIntMonth));
                Intent intent=new Intent(AddBreakfastActivity.this,ProvidersUpdateActivity.class);
                startActivity(intent);
                finish();
            }
            else {
                this.doubleBackToExitPressedOnce = true;
                Log.e("BOOLEANVALUE", String.valueOf(doubleBackToExitPressedOnce));
                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);
                return false;
        }
}
        return super.onKeyDown(keyCode, event);
    }

答案 1 :(得分:1)

以下是"按两次退出"片段:

boolean doublePressToQuit = false;    
@Override
        public void onBackPressed() {
            if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
                getSupportFragmentManager().popBackStack();
            } else {
                if (doublePressToQuit) {
                    DashBoardActivity.this.finish();
                } else {
                    this.doublePressToQuit = true;
                    Toast.makeText(this, getString(R.string.quit_notification), Toast.LENGTH_SHORT).show();

                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            doublePressToQuit = false;
                        }
                    }, 2000);
                }
            }
        }

答案 2 :(得分:0)

IF语句中加入onBackPressed()以覆盖SELECT last_insert_rowid() 事件。

答案 3 :(得分:0)

尝试以下代码,它适用于我

boolean firstBackPressed = false;
.
.
.
@Override
    public void onBackPressed() {
        if (!firstBackPressed) {
            firstBackPressed = true;
            Toast.makeText(MainMenu.this, "Press back again to exit", Toast.LENGTH_SHORT).show();
        } else {
            super.onBackPressed();
        }
    }

答案 4 :(得分:0)

请找到这个

  private boolean isShownExit;

 @Override
    public void onBackPressed() {

        if (drawerLayout.isDrawerOpen(Gravity.LEFT)) {
            drawerLayout.closeDrawer(Gravity.LEFT);
            isShownExit = false;
            return;
        } else {
            if (getSupportFragmentManager().getBackStackEntryCount() == 1) {              
                    if (!isShownExit) {
                        isShownExit = true;
                        showToast(this, "Press again to exit");
                    } else {
                        hideSoftKeyboardDialogDismiss(this);
                        startAnotherActivityHereWhichDoYouwant();
                }
            } else {
               getSupportFragmentManager().popBackStack();
            }
        }
    }