I'm trying to implement a navigation drawer, and I've been following https://developer.android.com/training/implementing-navigation/nav-drawer#java step by step. The only difference is that I am implementing everything inside a fragment instead of an activity directly.
As required by the documentation, I created a Toolbar and implemented Home button inside the onCreateView:
Toolbar toolbar = rootView.findViewById(R.id.toolbar);
((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);
ActionBar actionBar = ((AppCompatActivity) getActivity()).getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeAsUpIndicator(R.drawable.ic_menu_black_24dp);
The problem now is that when the home button is pressed, onOptionsItemSelected is not called at all. Following is how I have overridden my onOptionsItemSelected:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
//mDrawerLayout.openDrawer(GravityCompat.START);
Timber.d("Home Button Pressed");
mDrawerLayout.openDrawer(GravityCompat.START, true);
return true;
}
return super.onOptionsItemSelected(item);
}
Can anyone tell what is wrong? The GitHub link for this project is shown below if you want to see the rest of my code. Thank you! https://github.com/chao-li/GameCodex