Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the options menu of my Activity?

In some methods of my Activity I want to check the title of menu or know if it is checked or not. How can I get Activity's menu. I need something like this.getMenu()

like image 833
Bobs Avatar asked May 14 '12 07:05

Bobs


People also ask

Where is my activity option?

On your Android phone or tablet, go to myactivity.google.com. Scroll down to your activity. Filter your activity. You can filter by both date and product at the same time.

How do I get to the activity control page?

Go to your Google Account. On the left, click Data & privacy. Under "History settings," click an activity or history setting you want to manage. Turn the activity or history setting on or off.


2 Answers

Be wary of invalidateOptionsMenu(). It recreates the entire menu. This has a lot of overhead and will reset embedded components like the SearchView. It took me quite a while to track down why my SearchView would "randomly" close.

I ended up capturing the menu as posted by Dark and then call onPrepareOptionsMenu(Menu) as necessary. This met my requirement without an nasty side effects. Gotcha: Make sure to do a null check in case you call onPrepareOptionsMenu() before the menu is created. I did this as below:

private Menu mOptionsMenu;  @Override public boolean onCreateOptionsMenu(final Menu menu) {     mOptionsMenu = menu     ... }  private void updateOptionsMenu() {     if (mOptionsMenu != null) {         onPrepareOptionsMenu(mOptionsMenu);     } } 
like image 114
Dustin Avatar answered Sep 26 '22 18:09

Dustin


Call invalidateOptionsMenu() instead of passing menu object around.

like image 23
Diwakar Bhatia Avatar answered Sep 25 '22 18:09

Diwakar Bhatia