Creating menus in activities is a good choice but what would be if we are creating menus in each fragments ?
Basically a fragment depends on Activity life cycles and its parent activity features so if you want to create different different menu options in every fragment then first of all you have to create all these menu items in parent activity like this :
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.findItem(R.id.iconAdd).setVisible(false);
menu.findItem(R.id.iconOpt).setVisible(false);
menu.findItem(R.id.iconRefresh).setVisible(false);
menu.findItem(R.id.iconDone).setVisible(false);
menu.findItem(R.id.iconIntroRefresh).setVisible(false);
super.onCreateOptionsMenu(menu, inflater);
}
and in your fragment if you want to show iconAdd , iconOpt in menu then you can do like this :
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.findItem(R.id.iconAdd).setVisible(true);
menu.findItem(R.id.iconOpt).setVisible(true);
menu.findItem(R.id.iconRefresh).setVisible(false);
menu.findItem(R.id.iconDone).setVisible(false);
menu.findItem(R.id.iconIntroRefresh).setVisible(false);
super.onCreateOptionsMenu(menu, inflater);
}
0 Comment(s)