Clearing an ActionItem in refresh code - android

I am trying to clear an ActionItem in refresh code. I am using the ActiomBarSherlock component and I have my action item filtering but want to clear it when not in a menu driven event.
Can someone help me with the proper way to access it without holding onto my menu in a global variable? I tried to just reference the TextView directly but it is returning null.

There is nothing wrong with keeping a reference to a MenuItem to interact with it at a later time. You can also call invalidateOptionsMenu so that onCreateOptionsMenu will be called again and you can update the menu state.

Related

How do I only trigger onPrepareOptionsMenu and not onCreateOptionsMenu?

According to the Android menu documentation and a similar question, onPrepareOptionsMenu should be used to update menus dynamically. For Android 3.0 and higher, the documentation says:
When an event occurs and you want to perform a menu update, you must call invalidateOptionsMenu() to request that the system call onPrepareOptionsMenu()
However, when calling invalidateOptionsMenu(), the system calls both, onCreateOptionsMenu() and onPrepareOptionsMenu(). Resuming the activity also either calls both methods (if it was stopped in background) or none.
To me, this sounds like the differentiation between the two methods is obsolete. Is there any situation in which Android only calls onPrepareOptionsMenu() but not onCreateOptionsMenu()? Additionally, is there a way to manually request that Android only calls onPrepareOptionsMenu(), like it is implied in the documentation (cited below)?
[onCreateOptionsMenu()] is only called once, the first time the options menu is displayed. To update the menu every time it is displayed, see onPrepareOptionsMenu(Menu).
Thank you very much!
You are using Activity menu here, so assuming you have single activity app it will be only inflated once, during onCreateOptionsMenu - that's the scenario with a single call.
If you want to change it, then you have to invalidate the current one and inflate a new one - as it's an app's global menu it only make sense if you want to change its contents - you do that using onPrepareOptionsMenu and then inflate it again in onCreateOptionsMenu call.
But if you make lots of menu changes then it amy be better to use to use Fragment specific menus - they only live as long as the their fragment - menus
So to answer your specific question - you cannot NOT trigger the onCreateOptionsMenu. You can override it to do absolutely nothing but then the menu will not be inflated
EDIT - it seems I misunderstood the documentation as well. The key bit for onPrepareOptionsMenu is:
This is called right before the menu is shown, every time it is shown.
Which in practice means that every time you click on the Menu onPrepareOptionsMenu will be called, without calling onCreateOptionsMenu

Android: onResume update Radio button

I have a number of radio buttons on my menu. However, whenever onResume() is called, I found that it will reset all the radio buttons values to default. What I can do to keep it maintained to the previous status?
***Update
I know the status of the buttons. However, they are reset. I wanna find way to manually reset them. However, I found that I used the code
getMenuInflater().inflate(R.menu.menu_main, menu);
to try to get the menu and hence the MenuItem caused NullPointerException at the method onResume(). How can I get the menu and hence set the menuItems?
As soon as the user clicks on the radioButton, save its identifier/ID to your local storage.
(example: sharedPreferences). And on the activity onResume(), check if there's any saved status of clicked radioButton, and set it clicked.
Links provided for your reference.
I didn't provide a code because you also didn't provide any codes.

ActionBar.OnNavigationListener fires no matter which item was clicked in ACtionBar

I have a problem.
I have an action bar. It has a drop down list and it has an overflow button and another button some where in between.
Because I use a drop down, I am setting the action bar to use this mode:
mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
mActionBar.setListNavigationCallbacks(mActionBarNavigationAdapter, mOnNavigationListener);
mActionBar.setDisplayShowTitleEnabled(false);
My mOnNavifationListener is set to call a REST api depending on which item I chose in the drop down list.
This works as excepted.
The problem is, if I click on the overflow button to reveal extra options (such as settings or more importantly, Signout), I still get the navigation listener activated.
So I thought. Ok let's try to distinguish between the buttons using their position or id which are passed as parameters in onNavigationItemSelected method.
So I added an if statement that checks the position parameter. But...
It seems that the position of the overflow button is ALSO 0 (exactly like the position of the first item in the drop down list, so it passes the check and calls the REST api which is not good.
Also, the third button (not the drop down list or the overflow), has a position of 1 which effectively calls the other REST api...
I can't find a way to distinguish between the items in the action bar.
Any suggestions?
First, I'd strongly consider replacing your use of list navigation, as that is officially deprecated in the "L" Developer Preview and should remain deprecated in the next shipping version of Android.
I still get the navigation listener activated
I am not clear on what you mean by this. I also don't know which action bar you are referring to:
native API Level 11+?
appcompat_v7?
ActionBarSherlock?
Ideally, onNavigationItemSelected() will be called when your activity first appears, then if the user chooses something from the Spinner. If you are saying that it is being called at other points in time, while unfortunate, this can be worked around by simply keeping track, in your own data member, of where you are in the Spinner, and only doing work if the Spinner position actually has changed. So, for example, if you start off showing position 0, and the user taps something which causes onNavigationItemSelected() to be called again with position 0, you know that nothing needs changing, because you are still on position 0.

Closing Options Menu on orientation change in Android

this is my first post here so hello everyone and forgive me any mistakes common for newcomers.
In my actual Android 4.0 project im using Options Menu which is opened by clicking on one of ActionBar items. Items of my menu are asynchronously updated so in some cases it opens before changes has been made and this is proper behaviour for me. As we know, after user changes an orientation of the device, whole activity is recreated (same for its menu). This use case is properly handled in my code - state of Activity is saved.
Problem happens when user opens Options Menu and changes orientation when menu is still visible - menu gets recreated and shown. I would like to make it not appear after it has been created.
Is it even possible? I assume that i should do something either in onCreateOptionsMenu() or in onPrepareOptionsMenu() method.
You could use closeOptionsMenu() to close your OptionsMenu manually. If you want to open it at any later time, you could call openOptionsMenu().
I can think of a workaround. set a class variable true in onRestoreInstanceState. override onPrepareOptionsMenu return false if that variable is true and set that variable to false again. It should give you the behavior you want. comment if there's any problem, I'll post the code.

When does onCreateOptionsMenu happen in an ActionBar enabled activity?

I know the menu item will be set as action icons in the ActionBar.
I want to know exactly this onCreateOptionsMenu function, when does it called in the activity lifecycle.
From my test, it does not even after onResume
The documentation says the following:
public boolean onCreateOptionsMenu (Menu menu)
Initialize the contents of the Activity's standard options menu. You should place your menu items in to menu. This is only called once, the first time the options menu is displayed. To update the menu every time it is displayed, see onPrepareOptionsMenu(Menu).
Further explanation here: http://developer.android.com/reference/android/app/Activity.html#onCreateOptionsMenu%28android.view.Menu%29
And quoting what CommonsWare put on another related question:
The onCreate method is called first, and before it finishes onCreateOptionsMenu is called.
That will be true on devices and apps with an official Honeycomb-style action bar. If there is no action bar, onCreateOptionsMenu() should not get called until the user calls up the menu, typically by pressing the MENU button.
Link here: Android: When is onCreateOptionsMenu called during Activity lifecycle?
In my tests I discover that onCreateOptionsMenu is called after onResume as you can see too in this complete diagram of the lifecycle:
https://raw.githubusercontent.com/xxv/android-lifecycle/master/complete_android_fragment_lifecycle.png
I believe it is called at the same time as onCreate, just before the menu appears, in this case the actionbar
This is called the first time you touch the "options" dedicated button.
I'm trying to figure out when it's called on ActionBar supported also.
Also, you can request activity to do it, (but you need a Menu stub implementation)
activity.onCreatePanelMenu(Window.FEATURE_OPTIONS_PANEL, menu);

Categories

Resources