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_share
using 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 ie 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)
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);
在Main Fragment和Sub Fragment中,隐藏菜单onStop(),并显示菜单onResume()。
Try to call the Fragment super method to make use of the parent menu 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 android-actionbar 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.