I found the solution. Initially, the ActionBarDrawerToggle
Button (Home Button) is also a menu item. Thus, when we call:
mDrawerLayoout.onOptionsItemSelected()
we can put the code inside there.
Here is full the code for that:
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
if (myDrawerToggle.onOptionsItemSelected(item)) {
if (myDrawerLayout.isDrawerVisible(navDrawerRight) != false) {
myDrawerLayout.closeDrawer(navDrawerRight);
}
return true;
}
Notice I've put myDrawerLayout.isDrawerVisible(View drawer)
. That is because isDrawerOpened
only works when the drawer is fully opened or closed. It's better to use isDrawerVisible()
as it works when the drawer is in any state, opened, closed or in between.
Hope this helps someone out there.