Add Action button to the action bar for one fragment

My application has three tabs, and I use a pager to switch between tabs. Each tab has its own fragment. I added the Action and Action buttons to the action panel of the main action using the OptionsMenu methods. Now I want to add a new action button to the action bar, but only for the first tab and the first fragment, and I do not want it to appear on other fragments when they are displayed on the tabs.

I have this layout for the main menu, which was created in the main action -

<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 disappear when I go to other fragments on other pages / tabs. The remaining fragments do not have an option menu code in them.

Then I added this code to other fragments to remove 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 cleanup button does not appear on the action bar no matter which tabs are selected.

How to add an action button to the action bar for only one tab (i.e. a fragment) in my three tab (i.e. about three fragments)? It should appear only when this tab (fragment) is selected (displayed).

Thanks!

+2
android android-fragments
source share
3 answers

This is how I solved the problem of having an icon in the action bar for only one page in the pager view.

  • Create a complete menu, including an item that should be displayed only for a specific page, in the main activity in the usual way. Save the link to the menu item that changes for each page in the Application object.

  • Deploy onPageChangelistener as described above and set the menuItem to be visible for each page as needed. You access the menu element from the Application object. This keeps the menu items the way you want in a specific orientation of the phone, but will not work when you change the orientation of the phone. Save the current page position in the Application object. Be sure to initialize the position of the page in the application object on the first page (i.e. 0).

  • In onCreateOptionsMenu, in the main action, use the current page position (found in the Application object) to determine whether the menuI element should be visible or not. This will take care of the screen rotations.

0
source share

A simpler approach would be to include a menu item in the activity menu items and show and hide it pragmatically based on the selection of tabs. Get the link to the menu item in the onCreateOptionsMenu method and save it outside.

 @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu, menu); checkOut = (MenuItem) menu.findItem(R.id.action_abc); return true; } 

Use addOnPageChangeListener in the viewer to change the visibility of the menu item.

 mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { if (position == 0) { menuItem.setVisible(true); } else { menuItem.setVisible(false); } } @Override public void onPageScrollStateChanged(int state) { } }); 

I have not tried this myself.

+2
source share

When creating a newInstance of you TabFragement in the tab adapter, specify the tab section number in the fragment arguments.

 /** * Returns a new instance of this fragment for the given tab section * number. */ public static TabFragment newInstance(int sectionNumber) { TabFragment fragment = new TabFragment (); Bundle args = new Bundle(); args.putInt(ARG_SECTION_NUMBER, sectionNumber); fragment.setArguments(args); return fragment; } 

then in the OnCreate snippet, check the section number of the tab to determine if the snippet should have a options menu

only section number 0 inflates the menu: setHasOptionsMenu(sectionNumber==0?true:false);

 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); sectionNumber=getArguments().getInt(ARG_SECTION_NUMBER); setHasOptionsMenu(sectionNumber==0?true:false); } 

And inflate the Fragemnt menu in void onCreateOptionsMenu

 @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflator) { inflator.inflate(R.menu.first_tab_menu, menu); super.onCreateOptionsMenu(menu, inflator); } 
0
source share

All Articles