onBackPressed添加带导航抽屉的双击出口?

时间:2016-11-02 07:30:32

标签: java android android-navigation-drawer

我想编码如下..

boolean doubleBackToExitPressedOnce = false;
@Override
public void onBackPressed() {
    if (getFragmentManager().getBackStackEntryCount() > 0) {
        getFragmentManager().popBackStack();
    } else {
        Fragment fragment = getFragmentManager().findFragmentById(R.id.container);
        if (fragment instanceof MainHomeFragment) {
            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);
        }
        else {
            super.onBackPressed();
        }
    }
}

我的问题当抽屉打开,后退压力无法关闭抽屉.. 我想在下面添加代码......我应该把代码放在哪里?

DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
    if (mDrawerLayout.isDrawerOpen(GravityCompat.START)) {
       mDrawerLayout.closeDrawer(GravityCompat.START);
    }

2 个答案:

答案 0 :(得分:1)

 @Override
                public void onBackPressed() {DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
                if (drawer.isDrawerOpen(GravityCompat.START)) {
                    drawer.closeDrawer(GravityCompat.START);
                } else {
                   doExitApp()
    }

覆盖Activity

中的on backpressed方法
public void doExitApp(){
                if ((System.currentTimeMillis() - exitTime) > 2000) {
                    Toast.makeText(this, R.string.press_again_exit_app, Toast.LENGTH_SHORT).show();
                    exitTime = System.currentTimeMillis();
                } else {
                    finish();
                }
            }

答案 1 :(得分:0)

将此代码添加到onBackPress方法

        if (getFragmentManager().getBackStackEntryCount() > 0) {
    getFragmentManager().popBackStack();
} else {

            if (menu1.isOpened()) {

                menu1.close(true);

            } else {
                if (drawer_layout.isDrawerOpen(Gravity.LEFT)) {

                    drawer_layout.closeDrawer(Gravity.LEFT);

                    doubleBackToExitPressedOnce = false;

                } else {

                    if (doubleBackToExitPressedOnce) {
                        super.onBackPressed();
                        return;
                    }

                }
                this.doubleBackToExitPressedOnce = true;
                Toast.makeText(this, "Press again to exit", Toast.LENGTH_SHORT).show();

                new Handler().postDelayed(new Runnable() {

                    @Override
                    public void run() {
                        doubleBackToExitPressedOnce = false;
                    }
                }, 2000);
            }
             }