Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a hold of the ActionBar menu at UI setup time?

For some reason, onCreateOptionsMenu() is called AFTER onResume() in my app... Therefore, I just can't get a hold of the menu while I'm setting up my UI (between onCreate() and onResume()), which results in not being able to setup the corresponding action items for my ActionBar...

The only work-around I've found so far is to manually call invalidateOptionsMenu() right before onCreate() returns; that way onCreateOptionsMenu() is immediately called, I get a hold of the menu and then I can finally add the desired action items.

Has anyone experienced this issue? How are you supposed to programmatically setup your action items given onCreateOptionsMenu() is called after onResume()?

My app is running on JellyBean, it uses the built-in ActionBar (no ActionBarSherlock), android:minSdkVersion="14" and android:targetSdkVersion="16"

like image 989
Andrés Pachon Avatar asked Sep 20 '12 21:09

Andrés Pachon


People also ask

How do I open the pop up menu on Android?

Go to app > res > right-click > New > Android Resource Directory and give Directory name and Resource type as menu. Now, we will create a popup_menu file inside that menu resource directory. Go to app > res > menu > right-click > New > Menu Resource File and create a menu resource file and name it as popup_menu.

Where is ActionBar in Android?

Android ActionBar is a menu bar that runs across the top of the activity screen in android. Android ActionBar can contain menu items which become visible when the user clicks the “menu” button. In general an ActionBar consists of the following four components: App Icon: App branding logo or icon will be displayed here.

How to search for a menu item in the action bar?

Search an action in the action bar To search for a menu item in a menu you can use the findItem () method of the Menu class. This method allows to search by id. 2.4. Changing the menu The onCreateOptionsMenu () method is only called once. If you want to change the menu later, you have to call the invalidateOptionsMenu () method.

How to create a new Actionbar in Windows 10?

Right-click on the res folder and selects New -> Directory. Give the name “menu” to the new directory. Further, create a new Menu Resource File by right click on the menu directory. As the ActionBar is being created for the main Activity, type the name as “main” to the Menu Resource File.

What is the target activity in Actionbar?

In this example, the target activity is the MainActivity file. Further, the custom title, subtitle, and application logo are also defined in this file. Below is the proper code to design all mentioned items and to display a toast message when a user clicks on the items of ActionBar.

How to turn off the default action bar in the menu?

Create a new XML resource for your menu called mainmenu.xml in your menu resource folder (create one, if the folder does not exist). The resulting XML file should look similar to the following listing. 8.3. Turn off the default action bar from the styling Disable the default action bar one via the res/values/styles.xml file.


Video Answer


1 Answers

First consider that perhaps you shouldn't be doing this. It sounds like your idea might go against typical design patterns for Android. If your menu is changing in response to a user selection, for example, you should use contextual action mode instead.

  • From the Action Bar API Guide:

    As a general rule, all items in the options menu (let alone action items) should have a global impact on the app, rather than affect only a small portion of the interface. [...] So, even before deciding whether a menu item should appear as an action item, be sure that the item has a global scope for the current activity.

  • From the Menu API Guide:

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


Barring that, if you do want to change the menu items as you have described, you should make the change in onPrepareOptionsMenu(). When the event occurs that requires changing the menu items, put the relevant information into a field and call invalidateOptionsMenu(). Override onPrepareOptionsMenu() and check the value of the field to determine which menu items to add/remove.

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

More from the Menu API Guide:

You should use onCreateOptionsMenu() only to create the initial menu state and not to make changes during the activity lifecycle. If you want to modify the options menu based on events that occur during the activity lifecycle, you can do so in the onPrepareOptionsMenu() method.

This method passes you the Menu object as it currently exists so you can modify it, such as add, remove, or disable items. (Fragments also provide an onPrepareOptionsMenu() callback.)

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

  • On Android 3.0 and higher, the options menu is considered to always be 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 that the system call onPrepareOptionsMenu().

like image 65
quietmint Avatar answered Oct 09 '22 22:10

quietmint