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.
Related
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.
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.
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.
I have a spinner in my activity which is loaded with the database values. First time when the activity is loaded the spinner is selected to show the prompt text. When i go to the next activity and press back button then the spinner is selected to show the first item. But i need to show the prompt text when the activity is resumed.
How to achieve that? I need your help.
Plz post your first activities info (code and xml). This would help us give an accurate answer.
However, if you programmatically set your spinner with :
mSpinner.setSelection(0); // or whatever index you want to revert to
BEFORE you start up your 2nd activity. You should be good to go.
What you have done, to make spinner show the prompt text, do it in:
onResume method (Override it).
Also, if the thing that causes the spinner to show prompt text, is something that is not set yet, and you set it later(somewhere in onCreate, so it wont re-show it again in future),
you can override also onPause, to unset it.
Activity lifecycle is the key!
I have an android application where I want to display a LinearLayout when user clicks on options button(I am setting the linear layout's visibility to visible in onCreateOptionsMenu) and make it invisilble when user clicks on back button.
This works fine when I press the option button and then the back button-the view comes up and then goes away respectively.
The problem is when I press the option button again, the linearlayout does not show even though the visibility is being set to visible.
However, log tells me that the methos onCreateOptionsMenu is entered.
Why would this happen?
onCreateOptionsMenu is called only once per activity. In your case you have to code in onPrepareOptionsMenu. This will be called every time the user presses menu key.
Why do you want do this? For android users relevant reaction for menu button is option menu.
What about question, try use View.bringToFront() - it will bring view in front of all views of the same parent.