7

I want to have completely different menu options in different fragment.I followed this post.But my fragment menu is adding with the activity menu.But i don't want to have activity menus in some of my fragments.
In SlidingDrawerActivity:

  @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

In my fragment:

public Friends_Status_Comment_Fragment(){
        setHasOptionsMenu(true);
    }
  @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.menu_add_comment,menu);
        super.onCreateOptionsMenu(menu, inflater);

    }

The Activities items are adding with the menu of fragment.How to stop it???

1 Answer 1

17

I'm not sure if I underestand your problem - in your fragment you should clear menu and create new one - and don't call super :) something like this:

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater){
    menu.clear();
    inflater.inflate(R.menu.menu_add_comment,menu);
}
Sign up to request clarification or add additional context in comments.

2 Comments

will these previous menu will be back when i go back??
yes - if in your activity you will have onCreateOptionsMenu() menu will be recreated

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.