Why does onCreateOptionsMenu run multiple times?
<p>For some reason, my <code>onCreateOptionsMenu</code> and <code>onPrepareOptionsMenu</code> run twice (checked with a log input on the start of both methods). This happens for multiple fragments that I have, including some that are very basic (just inflating the menu, nothing else).</p>
<p>This is one of the <code>onCreateOptionsMenu</code>s that has this issue:</p>
<pre><code>@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.actionbuttons_add_removeall, menu);
    optionsMenu = menu;
}
</code></pre>
<p>What could cause these methods to be called multiple times (mostly twice)?</p>
<hr />
<h1>Update</h1>
<p>I found out this is being caused (in some way) by the <code>RecyclerView</code> I'm using. All the views that are having this issue use a RV, the view I mentioned before that didn't have this issue, indeed doesn't.
With this new info, which part of the RV could possibly be posing this issue?</p>
<h2>Update2</h2>
<p>I found out that I call <code>.invalidateOptionsMenu()</code> in the <code>getItemCount()</code> method in the <code>Adapter</code>. I <em>thought</em> that this would call <code>onPrepareOptionsMenu()</code>, but reading the docs, it seems like it calls <code>onCreateOptionsMenu()</code>. I'm probably getting <code>onCreate..()</code> and <code>onPrepare..()</code> reversed here, gonna check that out now.</p>
<h2>Update3</h2>
<p>I have just realized that I invalidate the options menu in my <code>RecyclerViewAdapter</code>, in the <code>getItemCount()</code> method, which obviously runs when the fragment is first created.</p>
<pre><code>@Override
public int getItemCount() {
    int tableSize = getTableSizeMethod();

    if (tableSize &lt; 1) {
        if (!AppManagerFragment.hideDeleteAllButton) {
            AppManagerFragment.hideDeleteAllButton = true;
            ((Activity) context).invalidateOptionsMenu();
            return 0;
        }
    } else {
        if (!AppManagerFragment.hideDeleteAllButton) {
            AppManagerFragment.hideDeleteAllButton = false;
            ((Activity) context).invalidateOptionsMenu();
            return tableSize;
        }
    }
}
</code></pre>