The submenu in the action bar does not work after support. InvalidateOptionsMenu is called

I have an action that extends the ActionBarActivity included in the revision of support package 18. I have a menu item that contains a submenu, and it works great when loading the application. However, if I call supportInvalidateOptionsMenu (), for some reason the submenu no longer appears. The linked code will be the xml for the menu

<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:myapp="http://schemas.android.com/apk/res-auto" > <item android:id="@+id/menu_search" android:icon="@drawable/ic_search" android:title="@string/menu_search" myapp:actionViewClass="android.support.v7.widget.SearchView" myapp:showAsAction="always|collapseActionView"/> <item android:id="@+id/menu_now_playing" android:icon="@drawable/ic_nowplaying" android:title="@string/menu_nowplaying" myapp:showAsAction="always"/> <item android:id="@+id/menu_station_overflow" android:icon="@drawable/ic_overflow" android:title="@string/more" myapp:showAsAction="always"> <menu> <item android:id="@+id/menu_favorite" android:icon="@drawable/ic_favorite" android:title="@string/favorite"/> </menu> </item> </menu> 

And then the code to create the menu

 @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.main_activity, menu); return super.onCreateOptionsMenu(menu); } 

I should note that this problem occurs on Gingerbread devices, but there is no problem with android 4.x. Does anyone know what could be happening here?

+4
source share
2 answers

Here is the work (on the same issue). Any menu items that need to be changed later are placed in instance variables, for example:

 private MenuItem stationMenuItem; @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.main_activity, menu); stationMenuItem = menu.findItem(R.id.menu_station_overflow); return super.onCreateOptionsMenu(menu); } public void doStuff(boolean menuVisible) { if (stationMenuItem != null) { stationMenuItem.setVisible(menuVisible); } } 

This is NOT an ideal solution, but something that will work before this will be fixed. Changes to menu items MUST occur in onPrepareOptionsMenu(Menu menu) after calling supportInvalidateOptionsMenu()

+2
source

Do not return super.onCreateOptionsMenu(menu); because it will always return false. Just return true.

0
source

Source: https://habr.com/ru/post/1497208/


All Articles