每个帖子都是导航抽屉,但由于我使用底部导航,我无法找到任何解决方案。搜索并尝试了所有主题。
这是我选择菜单项的选择器方法
try {
rs = currConn.createStatement().executeQuery(sql);
} catch (SQLException e) {
Logger.logError(e);
finally{
try {
currConn.close();
} catch (SQLException e1) {
Logger.logError(e1);
}
}
答案 0 :(得分:1)
你可以写
boolean isClickedTwice = false;
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
View v = null;
int id = item.getItemId();
switch (id){
case R.id.close:
//have to implement double click here.
if (isClickedTwice) {
this.finish();
}
isClickedTwice = true;
break;
}
final FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.main_container, fragment).commit();
return true;
}
});
答案 1 :(得分:1)
您必须创建一个全局布尔变量来检查双击功能,如:
boolean doubleBackToExitPressedOnce = false;
之后,在case R.id.close
if (doubleBackToExitPressedOnce) {
finishAffinity();
}
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);
答案 2 :(得分:0)
我看到你正在使用片段。你为此维护任何后栈跟踪吗?我建议你这样做,然后你实施双击退出应用程序。让我知道
allowedToExit是一个布尔值,用于在允许用户退出时跟踪用户。我们根据堆叠的碎片数量和用户按下两次来判断。我在我的onBackPressed方法中使用了这个逻辑来创建两次后退退出应用程序
//fragments remove logic
int backStackEntryCount = getSupportFragmentManager().getBackStackEntryCount();
// this is last item
if (backStackEntryCount == 1) {
if (allowedToExit)
finish();
else {
allowedToExit = true;
Util.showToast(BaseActivity.this,
"Press again to exit", false);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
allowedToExit = false;
}
}, 1000);
return;
}
}
// we have more than 1 fragments in back stack
if (backStackEntryCount > 1) {
Util.hideSoftKeyboard(BaseActivity.this);
onRemoveCurrentFragment();
} else
super.onBackPressed();