Good morning!
I'm having some troubles when I try to show correctly the option menu on the Action Bar.
This is what i have:
Main activity extends from ActionBarActivity. I've declared 3 fragments: A, B and C.
Inside B i've declared two fragments, B1 and B2. B2 extends from SUpporMapFragment. The B fragment navigation chosen is TAB_NAVIGATION. Both A and C have Standar navigation.
B2 fragment:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu, menu);
}
The problem is that the menu appears in A and C fragments when it should happen. On both fragments A and C i've the next:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(false);
}
The behaviour is the next. When i run my app, the A and C fragments has the option menu visible. When i go to B, if I'm in B1 and go back to A or C, the menu dissapears. If i go to B2 and go back to A or C, the menu is shown. I've debbuged it and when i click in the A or C fragment after being in B2 fragment, either fragments A or C calls the onCreateOptionsMenu from B2 fragment. Any help will be great!
Solved using control variables. I call onCreateOptionsMenu in my Main Activity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
if (posicionFragmento==1 && Listado_tiendas_mapas_fragment.seleccionFragmentoMenu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
}
return true;
}
Related
I have an Activity with a Toolbar that I set as the supportActionBar. From this Activity I have various Fragments each with a customized ActionBar. I am able to call menu.clear() to remove the existing Menu but I am however unable to add another Menu in the same Fragment. This seems strange because menu.clear() behaves just as I would expect, but when calling inflater.inflate(R.menu.my_custom_menu,menu); appears to do nothing.
Example Fragment where I wish to modify the supportActionBar:
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
actionBar = ((AppCompatActivity)getActivity()).getSupportActionBar();
mGroupViewModel =
ViewModelProviders.of(requireActivity()).get(GroupsViewModel.class);
}
...
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
//Inflating seems to do nothing.
Log.i(TAG,"IN THE ONCREATEOPTIONSMENU FOR FRAGMENT.");
inflater.inflate(R.menu.group_edit_toolbar,menu);
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public void onPrepareOptionsMenu(Menu menu) {
Log.i(TAG,"ONPREPARE OPTIONS MENU IN FRAGMENT.");
menu.clear();
super.onPrepareOptionsMenu(menu);
}
Clearly there is something I don't understand but I can't narrow down on what that problem is.
Would a better approach be to have each Fragment have their own Toolbar instead of all my fragments modifying the hosting activity's supportActionBar?
UPDATE
After further testing, I notice that if I try to assign a local MenuItem in my Fragment, I receive a null pointer exception unless I first inflate a menu in the Fragment itself. This is leading me to think that I am not hijacking control of the Activity's supportActionBar in the Fragment, but rather am trying to create a separate ActionBar for the Fragment. Would anyone be able to supplement my thinking here?
Fragment's menu callbacks:
MenuItem editItem;
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
Log.i(TAG,"IN ONCREATEOPTIONS");
menu.clear();
//MUST INFLATE MENU OTHERWISE WE GET NULL ERROR.
inflater.inflate(R.menu.home_actionbar,menu);
editItem = menu.findItem(R.id.action_edit_group);
Log.i(TAG,"edititem: "+editItem.getItemId());
super.onCreateOptionsMenu(menu, inflater);
}
// This is called every time the Menu opens.
#Override
public void onPrepareOptionsMenu(Menu menu) {
Log.i(TAG,"IN THE on prepare FOR FRAGMENT.");
menu.findItem(R.id.action_create_group).setVisible(false);
menu.findItem(R.id.action_create_group).setEnabled(false);
if(owner.equals(currUser)){
menu.findItem(R.id.action_edit_group).setEnabled(true);
menu.findItem(R.id.action_edit_group).setVisible(true);
} else {
menu.findItem(R.id.action_edit_group).setVisible(false);
menu.findItem(R.id.action_edit_group).setEnabled(false);
}
super.onPrepareOptionsMenu(menu);
}
onPrepareOptionsMenu is always called after onCreateOptionsMenu and there you're deleting your recently inflated menu!
Just delete onPrepareOptionsMenu and it should work fine.
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.
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 trying to show a button in the actionbar when a Fragment is shown and to hide the button when the other Fragment are shown.
I Override the onCreateOptionsMenu method:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.main, menu);
MenuItem item= menu.findItem(R.id.action_example);
item.setVisible(true);
super.onCreateOptionsMenu(menu,inflater);
}
And use setHasOptionMenu(true):
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
I have done a test and I noticed that initially the button doesn't appear in the other Fragment , but after I open the Fragment in which I put this code above, the button is shown also in the other Fragment.
What you are missing is removing the optionsMenu in the onDestroy of the fragment. The behaviour you describe is logical with your code: when the Fragment is created you also create the options menu. It will not automatically be destroyed when the Fragment is destroyed.