当我点击UpButton(actionBar的左箭头)时,它会打开我的抽屉菜单,并且onOptionsItemSelected方法不会收到任何内容。
如何阻止抽屉菜单拦截“按钮”按钮?
AndroidManifest.xml :
<activity
android:name=".ChildActivity"
android:theme="@style/AppTheme.NoActionBar"
android:parentActivityName=".ParentActivity">
ChildActivity - OnCreate :
// Toolbar
Toolbar toolbar = (Toolbar) findViewById(R.id.main_toolbar);
this.setSupportActionBar(toolbar);
ActionBar actionBar = this.getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
// Drawer Menu
DrawerLayout drawer = (DrawerLayout) this.findViewById(R.id.main_drawer);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.drawer_open, R.string.drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) this.findViewById(R.id.main_navigation);
navigationView.setNavigationItemSelectedListener(this);
ChildActivity - onOptionsItemSelected :
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
// This code is never executed
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
Ps :抽屉或upButton分开工作得很好。 我可以在ChildActivity中禁用/隐藏抽屉并在之后将其恢复,但我认为这不是正确的做法。