0

I added a refresh button to action-bar. When fragment loading that refresh button is creating in the action-bar. I have more than one fragment in my application, problem is when move between fragments and go back using back button using back button, action bar menu refresh button drawing several times. like thisenter image description here

following code I'm using

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

             final View rootView = inflater.inflate(R.layout.fragment_seebo_tv, container, false);
            setHasOptionsMenu(true);
}

@Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.seebo_tv, menu);
    }

in main activity

public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar actions click
    Fragment fragment = null;
    switch (item.getItemId()) {

          case R.id.action_refresh:
            fragment=new ProgressFragment();
            FragmentManager fragmentManagerProgress = getFragmentManager();
            fragmentManagerProgress.beginTransaction().replace(R.id.container, fragment).addToBackStack(null).commit();
            return true;
        default:
            return super.onOptionsItemSelected(item);           

    }

}

how can I fix this?

Nuwan Indika
  • 901
  • 4
  • 14
  • 27

1 Answers1

2

Try moving the call to setHasOptionsMenu(true); from onCreateView to onCreate instead.

Here's an example of a fragment adding a menu item: https://stackoverflow.com/a/31935887/798464

Community
  • 1
  • 1
Jon Finerty
  • 198
  • 1
  • 8