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.
Related
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();
}
I tried so many answers provided by various posts here but nothing worked for me.
Problem- I have navigation drawer that has 6 fragments but single activity. Everything worked fine till I changed 1st ranked fragment in drawer. I wanted Swipe tabs inside first fragment. So I used FragmentStatePagerAdapter.
Each fragment has its own menu along with MainActivity Menu.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Notify the system to allow an options menu for this fragment.
setHasOptionsMenu(true);
}
And inflated like this:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.story, menu);
}
Everything works fine. But When I visit other fragments in navigation drawer then it shows duplicate menu in toolbar. It creates more duplicates if there is space left in toolbar when I visit other fragments.
Try 1 : To solve this problem I initially used:
#Override
public void onPrepareOptionsMenu(Menu menu) {
menu.clear();
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.story, menu);
}
With this I don't get duplicate menu but now I don't see MainActivity menus.
Try 2:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
getActivity().invalidateOptionsMenu();
inflater.inflate(R.menu.story, menu);
}
With this I get both Fragment and Activity menu but Duplicates are there.
This should be easy to solve but I am not picking up a way to deal with this. Maybe I didn't understand the life cycle well?
My other approach- Implementing all menus in Fragments will do the trick but this should be our last option.
Solution to this - To maintain both Menu all I have to do is this (Very easy solution):
menu.clear();
inflater.inflate(R.menu.story, menu);
getActivity().getMenuInflater().inflate(R.menu.main, menu);
Problem 2 OnOptionsItemSelected method from 1st fragment is getting called in other fragments.
private void hideAllMenuItems() {
if (actionBarMenu != null) {
actionBarMenu.findItem(R.id.action_item1).setVisible(false);
actionBarMenu.findItem(R.id.action_item2).setVisible(false);
}
}
private void showMenuIcon() {
if (actionBarMenu != null) {
hideAllMenuItems();
if (currentFragment instanceof Fragment1)
actionBarMenu.findItem(R.id.action_item1).setVisible(true);
else if (currentFragment instanceof Fragment2)
actionBarMenu.findItem(R.id.action_item2).setVisible(true);
}
}
call shoeMenuIcon() each time new fragment load..
Hope you are looking for this
I am using drawerlayout in activity with several fragments. By clicking the navigation item, I switch the fragment this way:
switch (index) {
case 0:
if (checkinFragment == null) {
checkinFragment = new CheckinFragment();
ft.add(R.id.main_container, checkinFragment, "0");
} else {
ft.show(checkinFragment);
}
break;
case 3:
if (jianGuanFragment == null) {
jianGuanFragment = new JianGuanFragment();
ft.add(R.id.main_container, jianGuanFragment, "3");
} else {
ft.show(jianGuanFragment);
}
break;
}
ft.commitAllowingStateLoss();
Each fragment has a different menu resource file inflated in its onCreateOptionsMenu(Menu menu, MenuInflater inflater) method.
Steps:
1. When first enter the activity the menu in checkinFragment is shown.(All right)
2. Then switch to the jianGuanFragment, the menu in jianGuanFragment is shown.(Also right)
3. However, when switch back to the first fragment(checkinFragment), the actionbar menu isn't updated. The showing menu is still the one in jianGuanFragment. That's the problem.
And from the log I know the onCreateOptionsMenu in the showing fragment is called each time I switch to it. That's to say:
onCreateOptionsMenu is called but actionbar menu isn't updated.
Anyone can help me? Thanks a lot.
EDIT 1: add the code in onCreateOptionsMenu:
(1)in checkinFragment
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
LogUtils.e("TAG", "CheckinFragment onCreateOptionsMenu");
menu.clear();
inflater.inflate(R.menu.menu_setting, menu);
}
(2)in jianGuanFragment
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
LogUtils.e("TAG", "JianGuanFragment onCreateOptionsMenu");
menu.clear();
inflater.inflate(R.menu.menu_jianguan, menu);
}
EDIT 2: I use toolbar, not the old actionbar.
At last I find the solution:
Each time entering a fragment, re-set the toolbar as ationbar again.
#Override
public void onHiddenChanged(boolean hidden) {
super.onHiddenChanged(hidden);
if (!hidden) {
AppCompatActivity compatActivity = (AppCompatActivity) mActivity;
mToolbar = (Toolbar) compatActivity.findViewById(toolbarId);
compatActivity.setSupportActionBar(mToolbar);
}
}
Try this
1)in checkinFragment
#Override
public void onPrepareOptionsMenu(Menu menu) {
menu.clear();
LogUtils.e("TAG", "CheckinFragment onCreateOptionsMenu");
getActivity().getMenuInflater().inflate(R.menu.menu_setting, menu);
super.onPrepareOptionsMenu(menu);
}
Use invalidateOptionsMenu(). You need to use it where call to OnCreateOptionsMenu() is required.
EDIT:
getActivity().invalidateOptionsMenu() can be used if you want to call it from Fragment.
Hope that helps!!!
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);
}