Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide MenuItem in some Fragments

I using menu drawer which has more Fragments. In some Fragments I have menu item REFRESH but in some fragments I want hide this menu item (I don't want show menu but I don't want hide ActionBar).

I try add override onCreateOptionsMenu() to Fragment where I don't want show this menu item but I can not get it to work. I try many way see commented line in code. Does any idea where is problem? And last this menu item go to hide when I activate menu drawer when is called onPrepareOptionsMenu() in MainActivity but I need do this when I'm in Fragment.

Fragment where I want hide menu item REFRESH:

 public class FindPeopleFragment extends Fragment {
    public FindPeopleFragment(){}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_find_people, container, false);
        //setHasOptionsMenu(false);
        return rootView;
    }

    private Menu menu=null;
    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
    {
        inflater.inflate(R.menu.main, menu);
        this.menu=menu;
        menu.findItem(R.id.refresh).setVisible(false);
        getActivity().invalidateOptionsMenu();
        //setHasOptionsMenu(false);
        super.onCreateOptionsMenu(menu,inflater);
    }
}

MainActivity where is defined MENU DRAWER:

 //Slide menu item click listener
private class SlideMenuClickListener implements
        ListView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
                            long id) {
        // display view for selected nav drawer item
        displayView(position);
    }
}

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // toggle nav drawer on selecting action bar app icon/title
    if (mDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }
    switch (item.getItemId()) {
        case R.id.refresh:
            Toast.makeText(this, "Refreshing data...", Toast.LENGTH_SHORT).show();
            return true;
    }
    return super.onOptionsItemSelected(item);
}

 // Called when invalidateOptionsMenu() is triggered
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    // if nav drawer is opened, hide the action items
    boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
    menu.findItem(R.id.refresh).setVisible(!drawerOpen);
    return super.onPrepareOptionsMenu(menu);
}
like image 917
pavol.franek Avatar asked Feb 01 '14 13:02

pavol.franek


People also ask

How to hide menu item in android Fragment?

When you click the show button to open a fragment, you can see the fragment menu items ordered before activity menu items. This is because of the menu item's android:orderInCategory attribute value. When you click the hide button to hide the fragment. The fragment menu items disappear from the action bar also.

Is Fragment life cycle dependent on activity life cycle?

A fragment life cycle is closely related to the lifecycle of its host activity which means when the activity is in the pause state, all the fragments available in the activity will also stop. Fragments added to the Android API in Android 3.0 which API version 11 to support flexible UI on large screens.

How can we send some data from one fragment to another?

Step 1 − Create a new project in Android Studio, go to File ⇉ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. Step 3 − Create two FragmentActivity and add the codes which are given below.


3 Answers

In the fragment where you want to hide the Item

@Override
public void onPrepareOptionsMenu(Menu menu) {
    MenuItem item=menu.findItem(R.id.action_search);
    if(item!=null)
       item.setVisible(false);
}

and in onCreate() of your fragment

 setHasOptionsMenu(true);
like image 110
pratham kesarkar Avatar answered Oct 28 '22 18:10

pratham kesarkar


In the Fragment where you don't want to show any menu options, you need setHasOptionsMenu(false); in the onCreate(), like this:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(false);
}

However, the menu that is being shown that you would like to hide (REFRESH), belongs to MainActivity. That is why it is always shown. Since you want to control the menu at the Fragment level (and not show an Activity options menu), my suggestion is to delete the menu code from the Activity and implement it in your Fragment.

Activitys and Fragments can each have their own separate menus. See this link.

like image 41
Luis Avatar answered Oct 28 '22 20:10

Luis


please try this

@Override 
public void onPrepareOptionsMenu(Menu menu) {
    menu.clear();
}

and put this on your fragmen's onCreate()

setHasOptionsMenu(true);
like image 24
Sandy Akbar Avatar answered Oct 28 '22 20:10

Sandy Akbar