VK.N
VK.N

Reputation: 193

Menu in fragments

I have this scenario:

I have a activity, lets call itAcitivty1 with

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    return false;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    return false;
}

I open a fragment from Activity1 lets call it Fragment1 with:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    setHasOptionsMenu(true);

    View view = inflater.inflate(R.layout.layout, container, false); 

    return view;
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {

    if (menu != null){

        menu.clear();
    }

    if (!boolean) {

        inflater.inflate(R.menu.menu1, menu);

    } else {

        inflater.inflate(R.menu.menu2, menu);
    }
}

This fragment will be called again from activity as a new instance.

Based on the boolean in onCreateOptionsMenu() I'm deciding what menu should be loaded in the fragment so, during the second instance if I click on a menu item, I see the objects of first instance fragment.

I have no clue, why is this happening?

How is the workflow for displaying menu options...

Upvotes: 0

Views: 159

Answers (3)

stallianz
stallianz

Reputation: 185

In a case like this, you need to put the menu in activity and update the menu dynamically in onPrepareOptionsMenu()

Upvotes: 0

pRaNaY
pRaNaY

Reputation: 25312

You need to inflate menu in onCreateOptionsMenu(..) of your Activity1 and need to make return true to display menu.

 @Override
public boolean onCreateOptionsMenu(Menu menu) {
   //Your code here
    getMenuInflater().inflate(R.menu.main1, menu);//My menu
    return true;
}

After that you get menu in your fragment also.

Edits:

If you use single menu file and show / hide MenuItem of your menu. it will solve your problem. Add all menu in single file.by default all menu items are visible false using android:visible="false"

See example code:

  @Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    MenuItem item1 = menu.getItem(0);
    MenuItem item2 = menu.getItem(1);
    MenuItem item3 = menu.getItem(2);
    MenuItem item4 = menu.getItem(3);
   if(!boolean){
     //visible items which you want to show when boolean is false
     item1.setVisible(true);
     item2.setVisible(true);
    }
   else
    {
     //visible items which you want to show when boolean is true
     item3.setVisible(true);
     item4.setVisible(true);
    }


    super.onCreateOptionsMenu(menu, inflater);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.menuItem1) {

        return true;
    }
    if (id == R.id.menuItem2) {

        return true;
    }
   ...
    return super.onOptionsItemSelected(item);
}

Upvotes: 0

Hadi Satrio
Hadi Satrio

Reputation: 4292

if (menu != null){
    menu.clear();
}

That piece of code might be the root cause.

You're telling the system to clear the menu if it's null. Well, FYI, the menu will never be null in the first place; it is supplied by the system. It might have no items inside, but it'll never be null.

One way to check if a menu already contains an item (or more) is to call hasVisibleItems().

From the documentations:

public abstract boolean hasVisibleItems()

Returns True if there is one or more item visible, else false.

Therefore, this is how you should do it:

if (menu.hasVisibleItems()){
    menu.clear();
}

Upvotes: 2

Related Questions