Code example for Fragment

Methods: getChildFragmentManager, onCreate, setHasOptionsMenu

0
    public static class Menu2Fragment extends Fragment {
        Menu3Fragment mFragment3;
 
        @Override 
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setHasOptionsMenu(true);
 
            FragmentManager fm = getChildFragmentManager();
            FragmentTransaction ft = fm.beginTransaction();
            mFragment3 = (Menu3Fragment)fm.findFragmentByTag("f3");
            if (mFragment3 == null) {
                mFragment3 = new Menu3Fragment();
                ft.add(mFragment3, "f3");
            } 
            ft.commit();
        } 
 
        @Override 
        public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
            MenuItemCompat.setShowAsAction(menu.add("Menu 2"), MenuItem.SHOW_AS_ACTION_IF_ROOM);