0

I have a MainActivity on that I have a navigation drawer,Lists of navigation drawers are fragments. I want to have refresh button on one of my fragment,to add refresh button on action bar i added following code in my MainActivity

  @Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.activity_main_actions, menu);

    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // toggle nav drawer on selecting action bar app icon/title
    if (mDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }
    if (item.getItemId() == R.id.action_settings) {
        return true;
    } else if(item.getItemId() == R.id.action_refresh) {

        if(haveNetworkConnection(getApplicationContext()))
        {
            LoadAllProducts task = new LoadAllProducts(ListDepartmentSemesterActivity.this);
            task.execute();
        }else{
            Toast.makeText(getApplicationContext(), "No Internet Connection", Toast.LENGTH_LONG).show();
        }

        return true;
    }else {
        return super.onOptionsItemSelected(item);
    }
}

But the thing is on every fragment refresh button is visible and from any fragments i can refresh products,how to hide it on other fragments??? Navigation drawer MainActivity and Fragments share same action bar???

  • 1
    You can check this http://stackoverflow.com/questions/23178663/hide-show-action-bar-option-menu-item-for-different-fragments, worked for me. – Kalu Khan Luhar Sep 16 '14 at 13:27

2 Answers2

1

Define onOptionsItemSelected(MenuItem item) in fragment and add your refresh button to it. And insert this in your fragment:

@Override
public void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    setHasOptionsMenu(true);
    setRetainInstance(true);


}
Sini
  • 406
  • 1
  • 6
  • 16
0

if you want handle menu button in each fragment try this code

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.simple_calendar_view,
                container, false);
        // you code here 

        setHasOptionsMenu(true);
        return rootView;
    }

inside same fragment you want handle its menu item clicks

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.action_refresh:
             // you Custom code here 
             // Example i make method called Refresh_event
            Refresh_event();
            break;

        default:
            break;
        }
        return super.onOptionsItemSelected(item);
    }

if you need hide some menu item when you open slide menu navigation

in you activity that host fragments

           public void onDrawerClosed(View view) {  


                // calling onPrepareOptionsMenu() to show action bar icons
                supportInvalidateOptionsMenu();
            }

            public void onDrawerOpened(View drawerView) {


                // calling onPrepareOptionsMenu() to hide action bar icons
                supportInvalidateOptionsMenu();
            }

     /* *
     * Called when invalidateOptionsMenu() is triggered
     */
    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        // if nav drawer is opened, hide the action item(About App && Refresh)
        boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
        menu.findItem(R.id.about_app).setVisible(!drawerOpen);
        menu.findItem(R.id.refresh).setVisible(!drawerOpen);
        return super.onPrepareOptionsMenu(menu);
    }
Mina Fawzy
  • 20,852
  • 17
  • 133
  • 156