Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Toolbar, onCreateOptionsMenu() returns false, 'up' navigation not working

I'm using the new Toolbar on Lollipop (no support library). My activity has a list and if the list is empty, I don't want to show any options menu. My implementation looks like this:

in onCreate():

setActionBar((Toolbar) findViewById(R.id.toolbar));
getActionBar().setDisplayHomeAsUpEnabled(true);

And the menu method:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    if (tasks.isEmpty()) {
        return false;
    }

    getMenuInflater().inflate(R.menu.menu_tasks, menu);

    return true;
}

When the method inflates the menu and returns true, the 'up' navigation arrow works file; when the method returns false, the arrow is there, but clicking it doesn't do any thing. Is this some kind of an Android bug?

like image 457
wujek Avatar asked Oct 31 '22 11:10

wujek


1 Answers

As a work around you can just skip inflating the menu altogether and return true.

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    return true;
}

That being said, the documentation for onCreateMenuItems(Menu menu) suggests returning false as you initially tried to do should work. There is a bug tracking this issue right now: https://code.google.com/p/android/issues/detail?id=118700

like image 119
bigz Avatar answered Nov 15 '22 06:11

bigz