I manage some Fragment
's in my own ActionBarActivity
named MainActivity
. One Fragment
is shown at one time. The example should be simple.
The Fragment
which is showing should have got an option menu under certain conditions.
Here my code:
public class MainActivity extends ActionBarActivity{
...
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// The certain conditions. You might know the background. ;-)
final boolean hasMenu = mNavigationDrawer.isDrawerOpen() ? false : true;
// The reference to the fragment which is shown
mCurrentShownFragment.setHasOptionsMenu(hasMenu);
return super.onCreateOptionsMenu(menu);
}
...
}
Because of the invocation of mCurrentShownFragment.setHasOptionMenu(true)
MainActivity
's and Fragment
's onCreateOptionMenu(...)
is called two times.
First question: Why?
Second question: Is that fine?
Third question: If second question's answer is false. How could I prevent this?
Best regards, Barock