title: OnCreateOptionsmenu called once in viewpager with tablayout
body: <p>I have a rare problem. I have an activity that has tabs populated dynamically (up to 20 tabs). Each fragment has an implementation of <code>OnCreateOptionsMenu</code> and a menu which contains a bookmark icon that changes color when pressed. There is a class variable <code>menu</code> of type <code>Menu</code> which is set when fragment's <code>OnCreateOptionsMenu</code> is called. This is to enable me set the bookmark menu item outside the <code>onCreateOptionsMenu</code>(This is also done dynamically).</p>
  
  <p><strong>Problem</strong>:
  When the parent activity have only a single tab(One fragment instance), this implementation works fine. However with more than one tab, the app crashes with the error: 
  <code>Attempt to invoke interface method android.view.MenuItem android.view.Menu.findItem(int)</code></p>
  
  <p><strong>What I think</strong>: I think the problem is that the <code>OnCreateOptionsMenu</code> is called once i.e Class variable: <code>menu</code> is set only once for the first tab (fragment) hence when other tabs are added, <code>menu</code> is <code>null</code></p>
  
  <p><strong>Some codes</strong>: 
  Fragment</p>
  
  <pre><code>Menu menu;
  ...
  ...
  @Override
      public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState) {
   setMenuBookmark(ques_obj);
  }
  
  private void setMenuBookmark(QuestionAnswerList ques_obj){
          MenuItem menuBookmark = this.menu.findItem(R.id.action_bookmark);
  
          if (ques_obj.getIs_bookmarked().equals("1")) {
              menuBookmark.setIcon(R.drawable.ic_bookmark_white_24dp);
          } else {
              menuBookmark.setIcon(R.drawable.ic_bookmark_border_white_24dp);
          }
      }
  
  ...
  ...
  
  @Override
      public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
          inflater.inflate(R.menu.cbt_fragment_menu, menu);
          this.menu = menu;
  
          super.onCreateOptionsMenu(menu, inflater);
      }
  </code></pre>
  
  <p>P.S setHasOptionsMenu() is set to true in Fragment's OnCreate</p>