0

PS see the issue before voting down or marking as duplicate.Tried all the possible approaches to hide the menu item but none seems to work.

My options_menu.xml is as follows

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/menu_search"
      android:title="@string/search_title"
      android:icon="@drawable/ic_action_search"
      android:showAsAction="collapseActionView|ifRoom"
      android:actionViewClass="android.widget.SearchView" />
<item
    android:id="@+id/menu_share"
    android:icon="@android:drawable/ic_menu_share"
    android:showAsAction="always"
    android:title="Share"/>
</menu>

I am displaying this menu on a ParentFragment where I hide the menu_shareusing this code

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

     inflater.inflate(R.menu.options_menu, menu);
    menu.findItem(R.id.menu_share).setVisible(false);
}

This hides the Share option successfully. Now this Fragment opens a new Fragment i.e a Child Fragment.

In this child Fragment, I want to hide the Search item and show only the Share option. But using the same code does not help here.

   @Override
public void  onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.options_menu, menu);
    menu.findItem(R.id.menu_search).setVisible(false);

}

I even tried putting getActivity().invalidateOptionsMenu(); but no effect, the Actionbar shows both the menu items always. Pls help me figure out what is causing this.

ERROR LOG

E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: info.androidhive.slidingmenu, PID: 27321
java.lang.NullPointerException
        at info.androidhive.slidingmenu.HomeFragment.onPrepareOptionsMenu(HomeFragment.java:476)
        at android.app.Fragment.performPrepareOptionsMenu(Fragment.java:1794)
        at android.app.FragmentManagerImpl.dispatchPrepareOptionsMenu(FragmentManager.java:1964)
        at android.app.Activity.onPreparePanel(Activity.java:2665)
        at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:540)
        at com.android.internal.policy.impl.PhoneWindow.doInvalidatePanelMenu(PhoneWindow.java:881)
        at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:297)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:146)
        at android.app.ActivityThread.main(ActivityThread.java:5593)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
        at dalvik.system.NativeStart.main(Native Method)
Swapna Lekshmanan
  • 514
  • 10
  • 30
  • if you have no need then you can remove all the source from code from menu items else lotof solutions here – Amitsharma Apr 28 '15 at 09:56
  • Look at here http://developer.android.com/guide/topics/ui/menus.html or this http://www.androidhive.info/2011/09/how-to-create-android-menus/ – Amitsharma Apr 28 '15 at 09:57

3 Answers3

7

Instead of hiding a menu in onCreateOptionsMenu method, you can try hiding in onPrepareOptionsMenu in your parent activity for which your fragment/child fragments are a part of. Just check whether that fragment is visible (can be done by assigning a tag name to each fragment) and then write your code to hide a menu if you are in that fragment. I hope this will help you.

you can do something like this in onPrepareOptionsMenu method:

Fragment frag = getSupportFragmentManager().findFragmentByTag("YOURFRAGMENT");

    if (frag != null && frag.isVisible()) {
      menu.findItem(R.id.menu_search).setVisible(false);
    } else {
        menu.findItem(R.id.menu_share).setVisible(false);
    }
        return super.onPrepareOptionsMenu(menu);
Ravi Kabra
  • 1,284
  • 1
  • 11
  • 17
  • now my child Fragment shows 3 items > Share Search Share.. :(. new share has been put to the list – Swapna Lekshmanan Apr 28 '15 at 09:05
  • Hie swapna, have you tried clearing the menu and then looked onto setting up the visibility for both menu items? – Ravi Kabra Apr 28 '15 at 09:07
  • You can modify the conditions of fragment (if and else part) visibility with your child fragments too. Check which is your parent fragment and then inside it if u are calling a child fragment too. Modify that condition accordingly. That will work. – Ravi Kabra Apr 28 '15 at 09:11
  • if (frag != null && frag.isVisible()) { menu.clear(); getMenuInflater().inflate(R.menu.options_menu, menu); MenuItem item = menu.findItem(R.id.menu_search); item.setTitle("Search"); } else { menu.clear(); getMenuInflater().inflate(R.menu.options_menu, menu); MenuItem item = menu.findItem(R.id.menu_share); item.setTitle("Share"); } Have you tried like this? – Ravi Kabra Apr 28 '15 at 09:18
  • whenever i put `menu.clear` the app just crashes on start.. still trying different ways.. i thought of creating different menus for each fragment. but then the items start overlapping.. something is seriously wrong here – Swapna Lekshmanan Apr 28 '15 at 09:47
  • Please attach proper error log over here. I think you are missing something before clearing menu which is required. Henceforth i think for that reason your app is getting crashed as said by you, you are getting a NullPointerException. – Ravi Kabra Apr 28 '15 at 09:49
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/76426/discussion-between-swapna-lekshmanan-and-ravi-kabra). – Swapna Lekshmanan Apr 28 '15 at 09:54
0

in both Main Fragment and Sub Fragment, hide the menu onStop(), and show the menu onResume().

Thinsky
  • 4,226
  • 3
  • 13
  • 22
0

Try to call the Fragment super method to make use of the parent Activity.

In onCreate Fragment's method call setHasOptionsMenu(true);

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

Override onCreateOptionsMenu in your Fragment and use the MenuItem class to manipulate the items on your as you want, see code below.

public class NewsViewFragment extends Fragment {

    public NewsViewFragment() {
    }

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

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

        MenuItem item = menu.findItem(R.id.action_news_search);

        if (SOME_BOOLEAN_VALUE) {
            item.setVisible(false);
        } else {
            item.setVisible(true);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_news_view, container, false);
    }

}

Hope this help you.