Search code examples
androidandroid-fragmentsnavigationandroid-actionbarnavigation-drawer

getActionBar().setHomeAsUpIndicator() Breaking Navigation Drawer "Hamburger" Animation


In my application, I'm using a Navigation Drawer. I have given each item in the Navigation Drawer a different Icon for opening the Nav Drawer.

When I initially start the app, the drawer icon for the first fragment animates like normal. But when I click on another Nav Drawer Item, the animations break.

In my MainActivity, I have this code for toggling the nav drawer:

    getActionBar().setDisplayHomeAsUpEnabled(true);
    getActionBar().setHomeButtonEnabled(true);

    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
            R.drawable.ab_mytasks, R.string.drawer_open,
            R.string.drawer_close) {
        public void onDrawerClosed(View view) {
            getActionBar().setTitle(mTitle);
            invalidateOptionsMenu(); // creates call to
            // onPrepareOptionsMenu()
        }

        public void onDrawerOpened(View drawerView) {
            getActionBar().setTitle(mDrawerTitle);
            invalidateOptionsMenu(); // creates call to
            // onPrepareOptionsMenu()
        }
    };
    mDrawerLayout.setDrawerListener(mDrawerToggle);
    mDrawerToggle.syncState();

Then in each of my fragments, I have this code for setting the custom icon:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
    final ActionBar actionBar = getActivity().getActionBar();
    actionBar.setDisplayShowTitleEnabled(false);
    actionBar.setHomeAsUpIndicator(R.drawable.ab_mytasks);

}

I have tried putting in the same mDrawerToggle method as in my MainActivity into my fragments. But the app crashes when I use getActivity().invalidateOptionsMenu().

Here is an image representation of my problem:

1 = Animation works as normal
2 = Selected another fragment from Nav Drawer
3 = Original Fragment icon animation is broken

1 = Works as normal, 2=Selected another item, 3=Original Fragment broken


Solution

  • When you change the icon (setHomeAsUpIndicator) you will no longer get an animation.