Skip to content
This repository has been archived by the owner on Jan 13, 2018. It is now read-only.

OnPrepareOptionsMenu() doesn't get called #33

Open
aaronweihe opened this issue Apr 11, 2013 · 6 comments
Open

OnPrepareOptionsMenu() doesn't get called #33

aaronweihe opened this issue Apr 11, 2013 · 6 comments

Comments

@aaronweihe
Copy link
Contributor

If the device doesn't have physical menu button, OnPrepareOptionsMenu won't get called when I click on the overflow menu button.

But in the official Google ActionBar implementation, it gets called.

I looked at ActionBarSherlock, it seems to be a problem as well.

I tried let OnCreateOptionsMenu return true to get OnPrepareOptionsMenu called but no luck. See here.

@aaronweihe
Copy link
Contributor Author

The reason I want to get this work is I need to change the menu items at runtime. I use a different approach though. But it's not perfect.

@Cheesebaron
Copy link
Owner

I think because OnPrepareOptionsMenu is already overriden in LegacyActionBarActivity it will not be hit again in your activity which inherits from it. Will try to see if this can be fixed somehow :)

@aaronweihe
Copy link
Contributor Author

Actually OnPrepareOptionsMenu won't be hit even in LegacyActionBarActivity when I click on the overflow menu if the device doesn't have physical menu button. I tested on Nexus 7, running 4.2, and Moto Droid Razr, 4.0.4.

I suspect the reason is because we use Theme.Holo.NoActionBar on 4.0+, system assumes they don't have "ActionBar".

If I use Theme.Holo on 4.0+, OnPrepareOptionsMenu will be called in botn LegacyActionBarActivity and HomeActivity, but only once.

@JeanBedard
Copy link

I am also using OnPrepareOptionsMenu to control menu visibility according to some data context... do you have some other way to know when the user is touching the Menu button ?

@aaronweihe
Copy link
Contributor Author

No, I can't find a way to do this.

I used a different approach to get the same behavior. In my app, there is a "select all" function in the menu items to select all the items in the list view and "deselect all" to reverse, so I need to dynamically switch between two.

What I did is when the activity first launches, it only shows "select all". Then when all items are selected or "select all" is clicked, "select all" will be removed and "deselect all" will be add into menu items. It is the only way I can think of.

@jamesmontemagno
Copy link
Collaborator

So something I do is actually have "CreateLegacyBar" method in my code and based on the context it will put in different menu items. For instance if I have a list of items with check boxes I have 3 different menus
1.) Nothing Selected
2.) 1 item selected
3.) Multiple items selected.

Whenever the items checked changes on the list I actually go ahead and re-create my LegacyBar.

I did the initial implementation of the overflow so i am up for new ideas. Currently you must use some sort of LegacyBarActivity base class which will do cleanup oncreateoptionsmenu and onprepareoptionsmenu.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants