My app has three tabs and I am using a pager to switch between the tabs. Each tab has its own fragment. I added the Settings and Help action buttons to the action bar in the main Activity using the OptionsMenu methods. I now want to add a new action button to the action bar, but just for the first tab and first fragment, and I don't want it to appear on the other fragments when they are displayed in their tabs.
I have this layout for the main menu that is created in the main activity - <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity"> <item android:id="@+id/action_settings" android:title="@string/action_settings" android:orderInCategory="100" app:showAsAction="never" /> <item android:id="@+id/help" android:title="@string/help" android:orderInCategory="100" app:showAsAction="never" /> </menu> This menu displays correctly and works as expected. I have another menu, menu_prelaunch_fragment for the first fragment - <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <item android:id="@+id/clear_form" android:title="Clear" android:icon="@drawable/ic_action_action_delete" app:showAsAction="always" /> </menu> and I add it to the action bar using the following code in the first fragment - @Override public void onCreate(Bundle savedInstance) { Log.d(TAG, "onCreate"); super.onCreate(savedInstance); setHasOptionsMenu(true); } @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflator) { Log.d(TAG, "onCreateOptionsMenu"); inflator.inflate(R.menu.menu_prelaunch_fragment, menu); super.onCreateOptionsMenu(menu, inflator); } @Override public boolean onOptionsItemSelected(MenuItem item) { Log.d(TAG, "onOptionsItemSelected"); if (item.getItemId() == R.id.clear_form) clearFragmentData(); return super.onOptionsItemSelected(item); } The problem is that this added button does not go away when I go to the other fragments on the other pages/tabs. The other fragments do not have any options menu code in them. I then added this code to the other fragments to remove the one button. @Override public void onCreate(Bundle savedInstanceState) { Log.d(TAG, "onCreate"); super.onCreate(savedInstanceState); setHasOptionsMenu(true); } @Override public void onPrepareOptionsMenu (Menu menu) { menu.findItem(R.id.clear_form).setVisible(false); super.onPrepareOptionsMenu(menu); } But now the clear button does not appear in the action bar regardless of what tabs are selected. How do I add an action button to the action bar just for one tab (ie fragment) in my three tab (ie three fragment) app? It should only appear when that tab (fragment) is selected (displayed). Thanks! Mark -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To unsubscribe from this group and stop receiving emails from it, send an email to android-developers+unsubscr...@googlegroups.com. To post to this group, send email to android-developers@googlegroups.com. Visit this group at https://groups.google.com/group/android-developers. To view this discussion on the web visit https://groups.google.com/d/msgid/android-developers/CAEqej2MoYS7fzcXcT04jS8HWT-a78%2Brienr913OadraXwxsX-A%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.