In my application, I have a screen which has a menu on action bar on tap of which I show sub-menus which I need to put at runtime depending on the status of a process running in background.
e.g. Suppose, I am on screen SCREEN and my menu is MENU which has three sub-menus
MENU----> 1. SubMenu1
2. SubMenu2
3. SubMenu3
If a process PROCESS running in background is finished I want to display menus SubMenu1 and Submenu2 otherwise I’ll put only SubMenu3 excluding SubMenu1 & SubMenu2.
I can take this decision in onCreateOptionsMenu(Menu menu) method but if the user is on the SCREEN and the PROCESS completes, SubMenu3 should be shown instead of the previously shown SubMenus.
I believe this can be done (as Play Store app adds “My Apps” menu at runtime).
How can I achieve this?
You can implement
onCreateOptionsMenu(Menu)and create the options menu there based on the current state.Whenever that state changes you can call
invalidateOptionsMenu()to indicate that the options menu needs to be recreated. This will recreate the menu and again callonCreateOptionsMenu(Menu)as described in http://developer.android.com/reference/android/app/Activity.html#invalidateOptionsMenu%28%29