Clicking on the hamburger icon on the toolbar does not open the navigation box

I have this navigation box that works great. To refactor my code, I removed all onOptionsItemSelecteds in actions and made all actions inherit from the base action, which extends AppComplatActivity and implements all the necessary methods. After this click on the hamburger icon does not work, even if I have syncstate () and that's it.

Any clues why this is not working?

One of the activities:

 public class MainActivity extends BaseActivity implements SearchFilterFragment.OnFragmentInteractionListener { NavigationView navigationView; DrawerLayout drawerLayout; private Tracker mTracker; @Override protected void onResume() { super.onResume(); drawerLayout.openDrawer(GravityCompat.START); } @Override protected void onPostResume() { super.onPostResume(); mTracker.setScreenName("MainActivity" + "-----"); mTracker.send(new HitBuilders.ScreenViewBuilder().build()); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL); drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); drawerLayout.openDrawer(GravityCompat.START); navigationView = (NavigationView) findViewById(R.id.navigation_view_primary); navigationView.setNavigationItemSelectedListener(new NavigationDrawerListener(this)); setupToolbar(); Haftdong application = (Haftdong) getApplication(); mTracker = application.getDefaultTracker(); } private void setupToolbar() { // Show menu icon final ActionBar actionBar = getSupportActionBar(); actionBar.setDisplayHomeAsUpEnabled(true);// will make the icon clickable and add the < at the left of the icon. DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.navigation_drawer_open, R.string.navigation_drawer_close); mDrawerLayout.setDrawerListener(mDrawerToggle); mDrawerToggle.syncState();//for hamburger icon } @Override public void onFragmentInteraction(Uri uri) { } 

}

BaseActivity:

 public class BaseActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_base, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } 

}

+9
android navigation-drawer android-optionsmenu hamburger-menu
source share
1 answer

You are using a four-parameter constructor for an ActionBarDrawerToggle , which means you need to call the toggle onOptionsItemSelected() method in MainActivity onOptionsItemSelected() to open / close the box.

For example:

 @Override public boolean onOptionsItemSelected(MenuItem item) { if(mDrawerToggle.onOptionsItemSelected(item)) { return true; } return super.onOptionsItemSelected(item); } 

If you provide your own Toolbar — for example, as an ActionBar support (although there is no need to install it as such) — you can instead pass this Toolbar as the third argument in the call to the ActionBarDrawerToggle constructor. For example:

 Toolbar toolbar = findViewById(R.id.toolbar); ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); 

Opening / closing the box will then be handled by ActionBarDrawerToggle internally, and you will not need to access the onOptionsItemSelected() switch.

setDisplayHomeAsUpEnabled() also not needed for this setting, which is convenient if you do not want to set the Toolbar as a Toolbar ActionBar .

+20
source share

All Articles