How to hold the ActionBar menu while customizing the user interface?

For some reason, onCreateOptionsMenu() is called AFTER onResume() in my application ... Therefore, I just can not hold the menu while I adjust my interface (between onCreate() and onResume() ), which makes it impossible to set the corresponding elements actions for my ActionBar ...

The only thing I have found so far is to manually call invalidateOptionsMenu() just before returning onCreate() ; so that onCreateOptionsMenu() is called immediately, I get a menu hold, and then I can finally add the necessary elements.

Has anyone experienced this problem? How do you plan to programmatically configure your action items, data onCreateOptionsMenu() is called after onResume() ?

My application runs on JellyBean, it uses the built-in ActionBar (without ActionBarSherlock), android:minSdkVersion="14" and android:targetSdkVersion="16"

+6
source share
1 answer

First think that perhaps you should not do this. It looks like your idea might go against typical Android design patterns. If your menu changes depending on the user's choice, for example, you should use the context mode.

  • In the Action Panel API Guide :

    As a rule, all the elements in the options menu (not to mention the action elements) should have a global impact on the application, and not affect only a small part of the interface. [...] So, even before you select a menu item as an action item, make sure that the item has a global scope for the current activity.

  • In the API Guide menu :

    You should never change the items in the options menu based on the View currently in focus. When in touch mode (when the user does not use the trackball or d-pad), the views cannot focus, so you should never use focus as the basis for changing items in the options menu. If you want to provide menu items that are context sensitive for the View , use the Context Menu .


The prohibition is that if you want to change the menu items as you described, you must make changes to onPrepareOptionsMenu() . When an event occurs that requires changing menu items, put the appropriate information in the field and call invalidateOptionsMenu() . Override onPrepareOptionsMenu() and check the value of the field to determine which menu items to add / remove.

(It would also be useful to call invalidateOptionsMenu() and override onCreateOptionsMenu() to change which menu items should be shown, although this approach is not recommended.)

More details from the API menu guide :

You should use onCreateOptionsMenu() only to create the start menu, and not make changes during the operation life cycle. If you want to change the options menu based on events that occur during the life cycle of an activity, you can do this in onPrepareOptionsMenu() .

This method passes the Menu object to you because it currently exists, so you can modify it, for example, add, remove, or disable items. (Snippets also call onPrepareOptionsMenu() .)

  • On Android 2.3.x and lower, the system calls onPrepareOptionsMenu() every time the user opens the options menu (presses the "Menu" button).

  • In Android 3.0 and above, the options menu is considered to always open when menu items are presented in the action bar. When an event occurs and you want to perform a menu update, you must call invalidateOptionsMenu() to request a call to the onPrepareOptionsMenu() .

+6
source

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


All Articles