in v4.fragment, i set setHasOptionsMenu(true); and then in onCreateOptionsMenu i set inflater.inflate(R.menu.menu_fragment, menu);, everything is ok until i switch language.
when the app is running, and i press home-key, open setting, change locale language, select my running app from recently app list, the option menu in actionbar will increase duplicate menuitem, what's happen?
Gemini, I know it is late and you most probably have the answer already. The easiest way to fix this issue is to just add menu.clear();
public void onPrepareOptionsMenu(Menu menu) {
MenuInflater inflater = new MenuInflater(getActivity().getApplicationContext());
menu.clear();
super.onPrepareOptionsMenu(menu);
inflater.inflate(R.menu.myMenu, menu);
}
Related
I am trying to hide a MenuItem of my menu but without success.
This is the code that I have right now:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}
#Override
public boolean onPrepareOptionsMenu(Menu menu){
MenuItem item = menu.findItem(R.id.lastOption);
item.setVisible(false);
invalidateOptionsMenu();
return true;
}
but it still shows that option when I run the application.
Trying to find why this behaviour happened, I tried to set some breakpoints to my code and found that menu variable has a variable called mVisibleItems. In that variable I can see that the item that I tried to hide does not appear. It appears on the application, though.
So, I cannot understand why if it does not appear on the menu visible variables, it still is shown on the application.
Am I missing something?
Thanks in advance!
invalidateOptionsMenu();, calls onCreateOptionsMenu again. From the documentation
Declare that the options menu has changed, so should be recreated. The
onCreateOptionsMenu(Menu) method will be called the next time it needs
to be displayed.
so, after setting your item to false, you are inflating the layout of your menu, again
I suppose that this is not the better way to do this but I could not do it using onPrepareOptionsMenu.
As I need two types of menu depending of the user that had logged in (admin or user) I finally do this creating two types of menu for my application. One with some options and one without the options that I do not need for a normal user. Then, on onCreateOptionsMenu I inflate the menu that I need depending of the user that has logged in.
This is the code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
if("admin".equals(typeUser)){
getMenuInflater().inflate(R.menu.activity_main_admin, menu);
}else{
getMenuInflater().inflate(R.menu.activity_main_user, menu);
}
return true;
}
where typeUser is a String that I send from the Login Activity in which I set a bundle depending of the user that has logged in.
I also have created two activity_main layouts changing the menu option of the NavigationView on the XML:
app:menu="#menu/activity_main_admin" or app:menu="#menu/activity_main_user", depending on the layout.
Finally, I also have created a condition to set the layout on the onCreate method of MainActivity depending of the user that has logged in. This is the code to do this:
if("admin".equals(typeUser)){
setContentView(R.layout.admin);
}else{
setContentView(R.layout.user);
}
I want replace current menu of action bar with another when change action bar.
To achieve this I wrote:
getMenuInflater().inflate(R.menu.menu2, menu_bar);
But it add new menu at the rest of current menu.
What i want is remove current and add the new one.
How can I do that?
You can call invalidateOptionsMenu(), which will cause the system to call onPrepareOptionsMenu(Menu menu) again. Inside this method, just check which one of your two menus you should inflate, after calling menu.clear().
Note that invalidateOptionsMenu() has been introduced in API 11 (Honeycomb). If you need to target a lower API level, you can store your Menu object from onPrepareOptionsMenu(Menu menu) as an attribute in your Activity, and directly call clear() on it, then inflate the needed menu as you are already doing.
If you're doing it when changing fragments then:
(1) Create a menu file for fragment
(2) onCreate() method of Fragment set setHasOptionsMenu(true);
(3) Override onCreateOptionsMenu, where you'll inflate the fragment's menu and attach it to your standard menu.
#Override
public void onCreateOptionsMenu (Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu2, menu);
}
(4) Override onOptionItemSelected in your fragment for item handlers.
Check the link below for more details..
the answer below is correct just has a extra point that will said
You can call invalidateOptionsMenu(), which will cause the system to call onPrepareOptionsMenu(Menu menu) again. Inside this method, just check which one of your two menus you should inflate, after calling menu.clear().
Note that invalidateOptionsMenu() has been introduced in API 11 (Honeycomb). If you need to target a lower API level, you can store your Menu object from onPrepareOptionsMenu(Menu menu) as an attribute in your Activity, and directly call clear() on it, then inflate the needed menu as you are already doing.
point : at onPrepareOptionsMenu(Menu menu) should not call older menu item that lead to Error
public boolean onPrepareOptionsMenu(Menu menu) {
// Error -> menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
menu.clear();
getMenuInflater().inflate(current_menu, menu);
return super.onPrepareOptionsMenu(menu);
}
I am developing an app with actionbarsherlock. Because i need to add compatibility for device under 3.0. In main(first) activity i need to hide actionbar (for design issue). Also that activity has an option menu. Question is how can i add legacy menu for recent version of device which doen't have any hardware menu button. I have checked all the options but not working. I am adding some options here
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.layout.coredialer_menu, menu);
return true;
}
#manifest
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="10" />
I have solved my problem by removing actionbarsherlock.
I am a noob to android and i am trying to inflate two different menus depending on user selection. However, the menu isn't switching. The same menu inflates everytime regardless of what the user selects. I've tried and checked my if/else statmente with various parameters and the menu inflater still doesn't respond properly by only inflating the same menu. Any help is greatly appreciated.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
super.onCreateOptionsMenu(menu);
popUpMenu = getMenuInflater();
//popUpMenu.inflate(R.menu.cool_menu, menu);
if(mypodcast==null){
popUpMenu.inflate(R.menu.cool_menu, menu);
}else {
popUpMenu.inflate(R.menu.podcast, menu);
}
return true;
}
The same menu inflates everytime regardless of what the user selects.
That is because onCreateOptionsMenu() is not called each time you open the menu, from the documentation:
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)
If you want to change menus you need to do this in onPrepareOptionsMenu(). However I don't believe that you can or should inflate a new menu every time onPrepareOptionsMenu() is called. But you can combine the two menus and change the visibility of each menu item according to what you want in this method.
I read that it was possible to display a different menu for each tab on this guide.
I have 3 tabs initialized in MainActivity.
Even if I call onPrepareOptionsMenu() or onCreateOptionsMenu() in each included Activity, they are never executed.
I just succeed in displaying a menu on MainActivity's initialization...
MainActivity:
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.layout.menu_tab_1, menu);
return true;
}
This menu is shown.
One of my TabActivities:
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
menu.clear();
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.layout.menu_tab_2, menu);
return true;
}
This menu is not refreshed when I change the tab.
But I tried multiple combinations (onCreate / onPrepare, Override...) without success.
How do it properly?
Thanks
You have two options to do this.
First, you can override the onKeyDown method, and detect when the user presses the menu, and instead of letting the main activity handle it, dispatch the event to the active tab activity.
Else, you can use Fragments instead of activities inside your Tab host. The fragment mechanism is great to combine the Options Menus from multiple source (usually one activity and one or more fragments).