I have refined the Navigation Drawer Activity project template of Android Studio, which uses Toolbar, v7.app.ActionBarDrawerToggle and NavigationView instead of the NavigationDrawerFragment (and layout/fragment_navigation_drawer.xml).
According to Google's guidance and reference, I set up ActionBarDrawerToggle. I made it 1) instantiate in onCreate, 2) call syncState in onPostCreate and 3) call through to onConfigurationChanged and onOptionsItemSelected.
It is almost perfectly working except for one thing: the hamburger icon is showed as an arrow.
A similar issue can be found on the StackOverFlow, especially for this question. But the question is about the way using the old R.drawable.ic_drawer as hamburger which is not the material design (before 5.0 Lollipop) version. Moreover the question has no answer and the questioner commented he had solved without stating any solution.
After a while, I have found a solution accidentally. It is somewhat dirty. It is to call syncState in onCreate. Because it seems that, for some reason, onPostCreate is not called in my app. Actually, this dirty solution is used in an answer to the other problem.
But the official reference says to call syncState in onPostCreate. Why doesn't it work? Why doesn't my app call onPostCreate? This should be main cause of not being showed hamburger icon (instead being showed arrow).
Below is my code:
@Override
protected void onCreate(Bundle savedInstanceState) {
...
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerToggle = new ActionBarDrawerToggle(
this,
drawerLayout,
R.string.navigation_drawer_open,
R.string.navigation_drawer_close
) {
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
invalidateOptionsMenu(); // calls onPrepareOptionsMenu()
}
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
invalidateOptionsMenu(); // calls onPrepareOptionsMenu()
}
};
drawerLayout.setDrawerListener(drawerToggle);
navigationView = (NavigationView) findViewById(R.id.navigation_view);
navigationView.setNavigationItemSelectedListener(this);
drawerToggle.syncState(); // calling this here is somewhat a dirty solution
}
@Override
public void onPostCreate(Bundle savedInstanceState,
PersistableBundle persistentState) {
super.onPostCreate(savedInstanceState, persistentState);
drawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
drawerToggle.onConfigurationChanged(newConfig);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (drawerToggle.onOptionsItemSelected(item)) {
return true;
}
...
}
Here onPostCreate:
It should be this:
There are two types of onPostCreate:
You should have made a mistake to choose the former one when you override a method on Android Studio.