在我的应用程序中,我使用 navigation drawer menu
。当用户点击后退按钮时,我需要关闭此菜单并返回上一个活动。
我在下面写了代码:
@Override
public void onBackPressed() {
if (toNightSearchView.isSearchOpen()) {
toNightSearchView.closeSearch();
} else if (toNightDrawerLayout.isShown()) {
toNightDrawerLayout.closeDrawer(Gravity.END);
} else {
super.onBackPressed();
}
}
问题是当菜单关闭时,应用程序不会回到之前的活动。
我该如何解决这个问题?
答案 0 :(得分:0)
// use `isOpen()` to check the drawer state
if (toNightSearchView.isSearchOpen() || toNightDrawerLayout.isOpen()) {
//close if any of the views are open
if (toNightSearchView.isSearchOpen())
toNightSearchView.closeSearch();
if (toNightDrawerLayout.isOpen())
toNightDrawerLayout.closeDrawer(Gravity.END);
// Gravity.END is used for closing right drawer and start for left one
} else{
super.onBackPressed(); // go back to previous activity
}
或更好地使用isDrawerOpen
if (toNightSearchView.isSearchOpen() || toNightDrawerLayout.isDrawerOpen(Gravity.START)) {
if (toNightSearchView.isSearchOpen())
toNightSearchView.closeSearch();
if (toNightDrawerLayout.isDrawerOpen(Gravity.START))
toNightDrawerLayout.closeDrawer(Gravity.START);
} else{
super.onBackPressed(); // go back to previous activity
}