kinggg
kinggg

Reputation: 156

Toolbar Menu showing in wrong fragment

My main_activity have 2 fragments -> Parent and Child as follow :

https://imgur.com/ws9BRWR

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 :

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 :

  1. calling child fragment onCreateView

  2. calling parent fragment onCreateView

  3. calling child onCreateOptionsMenu

  4. 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 :

https://imgur.com/wYVdsvH

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 :(

Upvotes: 0

Views: 1201

Answers (2)

kinggg
kinggg

Reputation: 156

I found the answer from this link:
Set menus for multiple toolbars on android

If we running multiple toolbar menu within 1 activity (even in different fragment in my case)

  1. for the first toolbar, inflate is from onCreateOptionsMenu(), and callback is from onOptionsItemSelected()

  2. for the second toolbar, via we need to inflate the second menu toolbar and set the callback manually:

    toolbar2 = (Toolbar) findViewById(R.id.tool_bar_bottom);
    toolbar2.inflateMenu(R.menu.bottom_menu);//changed
    toolbar2.setOnMenuItemClickListener(new OnMenuItemClickListener() {
    
        @Override
        public boolean onMenuItemClick(MenuItem arg0) {
            if(arg0.getItemId() == R.id.item_id){
    
            }
            return false;
        } 
    });
    

Thank you @skadoosh.

Upvotes: 0

advice
advice

Reputation: 5998

Have you tried calling invalidateOptionsMenu() after you've added or replaced the Fragment?

Upvotes: 1

Related Questions