gpt4 book ai didi

java - onBackPressed 添加带有抽屉导航的双击退出?

转载 作者:行者123 更新时间:2023-11-30 00:54:44 28 4
gpt4 key购买 nike

我想按照下面的方式进行编码..

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

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

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

最佳答案

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

关于java - onBackPressed 添加带有抽屉导航的双击退出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40374514/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com