Problems implemeting Options Menu on Action Bar

153 views Asked by At

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!

1

There are 1 answers

0
n4h1n On BEST ANSWER

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;
}