What would be the best way to clear the focus in an android activity having a navigation drawer.
The problem it's that when the user has a textbox selected, if you open the navigation drawer the back event it's not consumed by the drawer, and the navigation goes back one step leaving the drawer opened.
Right now I solved using this on the activity, but i would like to know if someone use another solution where i don't need to use the ActionBarDrawerToggle events.
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
View window = findViewById(R.id.content_frame);
window.clearFocus();
}
Indeed it seems the drawer doesn't have focus when it's opened so it's onKeyUp
won't be called. I checked this in the host activity and in onBackPressed
DrawerLayout#hasFocus()
returns false if the activity has a view that gained focus - an EditText or something else.
I even tried to mess up with its descendantFocusability
property and that didn't work. I managed to fix this with a similar approach as you did: the drawer layout is requesting the focus:
mDrawerLayout.setDrawerListener(new DrawerLayout.SimpleDrawerListener() {
@Override
public void onDrawerOpened(View drawerView) {
mDrawerLayout.requestFocus();
}
});
You are clearing the focus from content ViewGroup, but I am not sure who gets the focus then, most probably no view.
Honestly speaking, this seems to me a bug in DrawerLayout as if it's opened, it should have the focus gained.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With