menu inflating calls multiple times at fragment's onCreateOptionsMenu

I use Fragments and when I switch to nested Fragment, which implements public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) my menu inflates quantity of times when I get to that nested Fragment. How can I avoid this? I also implement constructor of Fragment with methods:

setRetainInstance(true);
setHasOptionsMenu(true);

When I tried to implement siple solution as:

  • External SDCard file path for Android
  • Android Support Repo 46.0.0 with Android Studio 2.3
  • Got a stacktrace from Android Market that mentions a competitor's app
  • How to resolve the error “com.android.internal.R cannot be resolved” when I using the android file 'MultiAutoCompleteTextView.java',
  • Difference between base context, application context and activity context
  • Android SDK not found. Make sure that it is installed. If it is not at the default location, set the ANDROID_HOME environment variable
  •  @Override
     public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
         // TODO Add your menu entries here
       if(!isInflated)
             {
             inflater.inflate(R.menu.contacts_archive_menu, menu);
             isInflated = true;
             }
             super.onCreateOptionsMenu(menu, inflater);
    
     }
    

    but my menu wasn’t inflate after the screen rotation.

    Related posts:

    Activity transition in Android
    Get the URI of an image stored in drawable
    How to play sounds at accurate periods of time across different devices in Android
    android layout with visibility GONE
    Long press list item to open pop up menu
    Changing coding style due to Android GC performance, how far is too far?
  • onAttach() not called in Fragment
  • How to change color of hamburger icon in material design navigation drawer
  • Android Studio proguard handling in multi-library projects
  • How do I write an android JUnit test when my activity relies on extras passed through an Intent?
  • Performing a UPNP scan is not returning the Philips Hue Bridge
  • How does facebook verify app authenticity with key hash?
  • 3 Solutions collect form web for “menu inflating calls multiple times at fragment's onCreateOptionsMenu”

    I solved it simply by clearing menu before ionflating of it:

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

    Just check the count of menu items. Meaning menu.size()==0 ,no menu items are present,then inflate with layout menu,else don’t inflate at all.

     @Override
     public void   onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
          if (menu.size() == 0)
          inflater.inflate(R.menu.call_menu, menu);
          super.onCreateOptionsMenu(menu, inflater);
    
     }
    

    Use before replace.

    fragment = new EditMyProfile();
    android.support.v4.app.FragmentTransaction fragmentTransactionEditProfile =
    getSupportFragmentManager().beginTransaction();
    getSupportFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
    fragmentTransactionEditProfile.replace(R.id.frame, fragment);
    fragmentTransactionEditProfile.commit();

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.