My main_activity have 2 fragments -> Parent and Child as follow :
each fragment should have their own actionbar and their own menu.
my question are : why is the menu that supposedly shown in child fragment mixed together into the parent fragment?
Child-Menu-1 and Child-Menu-2 supposed to be inside menu in child fragment
this is what i have done :
fragmentParent :
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_parent, container, false);
setHasOptionsMenu(true);
Toolbar myToolbar = (Toolbar) view.findViewById(R.id.tb_parent);
((AppCompatActivity)getActivity()).setSupportActionBar(myToolbar);
((AppCompatActivity)getActivity()).getSupportActionBar().setTitle("PARENT");
//((AppCompatActivity)getActivity()).getSupportActionBar().setDisplayShowTitleEnabled(false);
return view;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu_parent, menu);
}
fragmentChild :
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_child, container, false);
setHasOptionsMenu(true);
Toolbar myToolbar = (Toolbar) view.findViewById(R.id.tb_child);
((AppCompatActivity)getActivity()).setSupportActionBar(myToolbar);
((AppCompatActivity)getActivity()).getSupportActionBar().setTitle("CHILD");
//((AppCompatActivity)getActivity()).getSupportActionBar().setDisplayShowTitleEnabled(false);
return view;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu_child, menu);
}
what i have tried already :
move the getSupportActionBar into onViewCreated <--- same result
move the inflate to onPrepareOptionsMenu <--- same result
i noticed that if i remove the menu from the parent fragment, the menu on the child show up correctly, problem only if i try to load each with their own menu.
when i debug the apps, the sequence called are :
calling child fragment onCreateView
calling parent fragment onCreateView
calling child onCreateOptionsMenu
calling parent onCreateOptionsMenu
so i'm suspecting the problem is in the onCreateOptionMenu where the inflater inflate the wrong fragment.
i already do a lot of searching inside SO and google, but no question seems similar to my case.
thank you very much in advance!!
==========================================================================
further examination and testing... so i commented out the creating of the toolbar from the parent fragment :
fragmentParent :
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_parent, container, false);
setHasOptionsMenu(true);
//Toolbar myToolbar = (Toolbar) view.findViewById(R.id.tb_parent);
//((AppCompatActivity)getActivity()).setSupportActionBar(myToolbar);
//((AppCompatActivity)getActivity()).getSupportActionBar().setTitle("PARENT");
//((AppCompatActivity)getActivity()).getSupportActionBar().setDisplayShowTitleEnabled(false);
return view;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu_parent, menu);
}
fragmentChild :
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_child, container, false);
setHasOptionsMenu(true);
Toolbar myToolbar = (Toolbar) view.findViewById(R.id.tb_child);
((AppCompatActivity)getActivity()).setSupportActionBar(myToolbar);
((AppCompatActivity)getActivity()).getSupportActionBar().setTitle("CHILD");
//((AppCompatActivity)getActivity()).getSupportActionBar().setDisplayShowTitleEnabled(false);
return view;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu_child, menu);
}
Result :
all menu item show up in the child fragment, why is the parent fragment onCreateOptionMenu getting called while there's no toolbar is declared?
i'm hitting wall here :(