Arrow is showed instead of the material design version hamburger icon. Why doesn't syncState in onPostCreate work?

痞子三分冷 提交于 2019-12-08 02:27:27

问题


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;
    }

    ...

}

回答1:


Here onPostCreate:

@Override
public void onPostCreate(Bundle savedInstanceState,
        PersistableBundle persistentState) {
    super.onPostCreate(savedInstanceState, persistentState);
    drawerToggle.syncState();
}

It should be this:

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    drawerToggle.syncState();
}

There are two types of onPostCreate:

  1. Activity's onPostCreate with two arguments.
  2. AppCompatActivity's onPostCreate with a single argument.

You should have made a mistake to choose the former one when you override a method on Android Studio.



来源:https://stackoverflow.com/questions/32787036/arrow-is-showed-instead-of-the-material-design-version-hamburger-icon-why-doesn

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!