waleedAhmad1 / google-glass-api

Automatically exported from code.google.com/p/google-glass-api
0 stars 0 forks source link

onPrepareOptionsMenu is not called when the menu is empty or items hidden #492

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?

Handle menu callback for a CardScrollView:

1.
mCardScrollView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        currentPosition = position;
        openOptionsMenu();
    }
});

2.
@Override
public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.share_menu, menu); //fake call
    return true;
}

3.
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    //menu.clear(); // Doesn't work on XE16, onPrepareOptionsMenu is not called anymore when menu is empty. It's the case for the 3 first screen in my CardScrollView

    switch (currentPosition) {
            case MENU_BARCODE_CONTENT:
            case MENU_GENERAL:
            case MENU_NUTRITIONAL_FACTS:
                break;
            case MENU_COUPON:
                getMenuInflater().inflate(R.menu.coupon_menu, menu);
                break;
            case MENU_SHARE:
                getMenuInflater().inflate(R.menu.share_menu, menu);
                break;
    }

    return super.onPrepareOptionsMenu(menu);
}

What is the expected output? What do you see instead?
onPrepareOptionsMenu should be called everytime "openOptionsMenu" is called 
even if there is not items in the menu or if the items are hidden.

What version of the product are you using? On what operating system?
XE16, everything was working fine on XE12

Please provide any additional information below.
It's not the way the menu are intended to work, but it's the only way to have a 
different menu on different card in a CardScrollView. I would be happy if I 
could have a quick workaround.

Original issue reported on code.google.com by jj.Sarra...@gmail.com on 28 Apr 2014 at 9:28

GoogleCodeExporter commented 9 years ago
Hello,

I just tried on XE16.2 and have not been able to reproduce this issue. Could 
you try and let us know?

Also, could you explain what you are trying to achieve? There might be better 
workaround for your use-case such as checking the "position" in your 
OnItemClickListener and only opening the options menu when necessary.

Best,
Alain

Original comment by ala...@google.com on 29 Apr 2014 at 8:12

GoogleCodeExporter commented 9 years ago
Hello Alain,

You will find attached a project example that should allow you to reproduce my 
issue.
To reproduce:
1. Build and deploy the project on glass
2. on the glass swipe to "Hello 2" screen
3. Tap, the menu will appear
4. Swipe down for back
5. Go to another screen
6. Tap for menu, nothing happens
7. Go back to "Hello 2" screen
8. Tap for menu, nothing happens as there is no items in the menu (removed on 
step 6).

I'm trying to display some information in several screens about a previously 
scanned product in a CardScrollView. For example I will have:
Card 1: General product info
Card 2: Allergens
Card 3: Expiration date...

If I want a specific action related to the "Expiration date" screen, I need to 
implement something like I did. Have you any better idea ?

Salutations de Suisse ;-)
Jean-Jérôme

Original comment by jj.Sarra...@gmail.com on 12 May 2014 at 12:30

Attachments:

GoogleCodeExporter commented 9 years ago
Hello,

Seems your use-case should be handled differently by going with one of those 
two options:
  * Check the position in the OnItemClickListener#onItemClick method and only open the options menu when it is equal to 2.
  * Do not clear the menu in "onPrepareOptionsMenu" but instead set the menu as visible and enabled when required (check the Timer's MenuActivity to see how it can be done: https://github.com/googleglass/gdk-timer-sample/blob/master/src/com/google/android/glass/sample/timer/MenuActivity.java#L99).

Best,
Alain

Original comment by ala...@google.com on 13 May 2014 at 10:50