My android app contains one main activity, two Fragment and one Dialog Fragment.
Main activity has an option menu.
Main Fragment use the main activity's option menu.
Second fragment has another option menu.
From second fragment, i can open the Dialog Fragment which contain
another set of option menu.
But when close the Dialog Fragment, option menus of all other fragment and activity are changed to that of Dialog Fragment.
MainActivity.java
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return super.onCreateOptionsMenu(menu);
}
SecondFragment.java
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.clear();
getActivity().getMenuInflater().inflate(R.menu.fragment_second, menu);
super.onCreateOptionsMenu(menu, inflater);
}
MyDialogFragment.java
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.clear();
getActivity().getMenuInflater().inflate(R.menu.fragment_dialog, menu);
super.onCreateOptionsMenu(menu, inflater);
}
Call invalidateOptionsMenu() in your second fragment.
Docs:
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.
In SecondFragment:
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
getActivity().invalidateOptionsMenu();
}
Related
I have menuItem in fragment A, the menuItem.setIcon work fine the first time, but when I Add fragment B (not replace) and go back to fragment A, the menuItem.setIcon is not working anymore!
here is my code on fragment A, the buttonClicked method work perfectly before I add the fragment B and go back to fragment A
private MenuItem menuItem;
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_main, menu);
menuItem = menu.findItem(R.id.action_item);
super.onCreateOptionsMenu(menu, inflater);
}
public void buttonClicked() {
if (condition)
menuItem.setIcon(getResources().getDrawable(R.drawable.ic_action));
else
menuItem.setIcon(getResources().getDrawable(R.drawable.ic_action_checked));
}
you need to call Activity.invalidateOptionsMenu(); to make any menu changes.
I have MainActivity and it's have fragment. I added toolbar from MainActivity;
MainActivity
private Toolbar mToolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mToolbar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
When I was open any fragment from MainActivity then that fragment use own menu file
In fragment
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.custom_menu, menu);
super.onCreateOptionsMenu(menu, inflater);
}
As you can see above I inflated custom menu in onCreateOptionsMenu. But It doesn't work.
after
toolbar.getMenu().clear();
add toolbar.inflateMenu(R.menu.menu_blank);
and I am not set toolbar as acionbar,Maybe you should user getActionbar to do something.
I'm not entirely sure this is what you're asking but I think you want to be able to change the options in the menu, dynamically. You can do the following
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
if (mHideSomething) {
MenuItem myItem = menu.findItem(R.id.action_something);
myItem.setVisible(false);
} //otherwise it will show as usual
return true;
}
Then when you want to change something in the menu...
mHideSomething = true;
supportInvalidateOptionsMenu();
EDIT
Now I understand you're just adding odd behaviour when overriding. You can still do the following (although it seems like these items just shouldn't be part of main menu if they're not relevant always)
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.custom_menu, menu);
super.onCreateOptionsMenu(menu, inflater);
MenuItem mainMenuItemToRemove = menu.findItem(R.id.action_something);
mainMenuItemToRemove .setVisible(false);
}
The only problem with the above is it makes assumptions about what is available in the main menu even though the fragment should be reusable. A better solution would be to pass in an interface to the fragment to call back to the activity and let the activity have control. Better still, update the activity logic and never inflate the main menu at all if not required.
I need to remove the inflated menu after I return to Fragment 1 from Fragment 2 after some action.
I clear the menu after I make the popBackStack, but nevertheless the menu item can't be deleted.
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.clear();
if (savedItemsExist())
inflater.inflate(R.menu.menu_saved_filters, menu);
else
{
Log.i(TAG + " onCreateOptionsMenu", " going to delete");
menu.clear();
}
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public void onPrepareOptionsMenu(Menu menu) {
menu.clear();
if (savedItemsExist())
getActivity().getMenuInflater().inflate(R.menu.menu_saved_filters, menu);
else{
menu.clear();
}
super.onPrepareOptionsMenu(menu);
}
If you have menu items tied to a Fragment's lifecycle, you should instead use setHasOptionsMenu(true) on your Fragment and then override onCreateOptionsMenu() in the Fragment, inflating your fragment's menu.
Maybe that is hack solution, but issue solved by replacing the Fragment 2 by Fragment 1 instead of popBackStack()
I want to have completely different menu options in different fragment.I followed this post.But my fragment menu is adding with the activity menu.But i don't want to have activity menus in some of my fragments.
In SlidingDrawerActivity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
In my fragment:
public Friends_Status_Comment_Fragment(){
setHasOptionsMenu(true);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_add_comment,menu);
super.onCreateOptionsMenu(menu, inflater);
}
The Activities items are adding with the menu of fragment.How to stop it???
I'm not sure if I underestand your problem - in your fragment you should clear menu and create new one - and don't call super :) something like this:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater){
menu.clear();
inflater.inflate(R.menu.menu_add_comment,menu);
}
I have one activity with 3 fragments. I want to hide menu which is created in activity and show the fragment 2 alone.
I know how to hide menu which is created by other fragments. Is it possible to hide the Activity menu on fragments?
When you override onCreateOptionsMenu you can clear the menu from there.
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.clear();
inflater.inflate(R.menu.menu, menu);
super.onCreateOptionsMenu(menu, inflater);
}