title: Viewpager with different menu and common toolbar not working
body: <p>I have tabs in my app. Each tab is of different fragments and has different menu. Below is the layout which I am using </p>
  
  <pre><code>    &lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      android:id="@+id/drawer_layout"
      android:layout_width="match_parent"
      android:layout_height="match_parent"&gt;
  
      &lt;android.support.design.widget.CoordinatorLayout
          android:layout_width="match_parent"
          android:layout_height="match_parent"&gt;
  
          &lt;android.support.design.widget.AppBarLayout
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:background="@color/background"&gt;
  
              &lt;include
                  layout="@layout/toolbar"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  app:layout_scrollFlags="scroll|enterAlways" /&gt;
  
  
          &lt;/android.support.design.widget.AppBarLayout&gt;
  
          &lt;com.CustomViewPager
              android:id="@+id/view_pager"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              app:layout_behavior="@string/appbar_scrolling_view_behavior" /&gt;
  
          &lt;android.support.design.widget.TabLayout
              android:id="@+id/tab_layout"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_gravity="end"
              android:background="@color/background"
              app:layout_anchor="@id/view_pager"
              app:layout_anchorGravity="bottom|center_horizontal"
              app:layout_behavior="com.widget.ScrollTabBehavior"
              app:tabBackground="@color/background"
              app:tabIndicatorColor="@color/toolbar"
              app:tabIndicatorHeight="0dp"
              app:tabMode="fixed"
              app:tabPaddingEnd="0dp"
              app:tabPaddingStart="0dp" /&gt;
  
      &lt;/android.support.design.widget.CoordinatorLayout&gt;
  
      &lt;RelativeLayout
          android:id="@+id/left_drawer"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:layout_gravity="start"
          android:layout_marginLeft="-64dp"
          android:layout_marginStart="-64dp"
          android:background="@color/toolbar"
          android:choiceMode="singleChoice"
          android:divider="@android:color/transparent"
          android:dividerHeight="0dp"&gt;
  
          &lt;ImageButton
              android:id="@+id/close_btn"
              android:layout_width="50dp"
              android:layout_height="50dp"
              android:layout_alignParentEnd="true"
              android:layout_alignParentRight="true"
              android:layout_alignParentTop="true"
              android:background="@android:color/transparent"
              android:padding="5dp"
              android:src="@drawable/close_icon" /&gt;
  
          &lt;view
              android:id="@+id/drawerlist"
              class="android.support.v7.widget.RecyclerView"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_below="@+id/close_btn" /&gt;
  
      &lt;/RelativeLayout&gt;
  &lt;/android.support.v4.widget.DrawerLayout&gt;
  </code></pre>
  
  <p>Now with every Fragment in <code>onCreate()</code> I have mentioned <code>setHasOptionsMenu(true);</code> further more. I have overriden the function <code>onCreateOptionsMenu()</code> in each fragment which has <code>menu.clear();</code> at first then calling it's super constructor then inflating fragment's own menu xml. But result I am getting is something like this -</p>
  
  <ul>
  <li>Suppose there are 5 tabs. 2nd and 3rd tab has viewpager within each consist of two more fragments within</li>
  <li>1st tab has no menu</li>
  <li>2nd tab has menu_2 (only for 2nd child fragment)</li>
  <li>3rd tab again has no menu</li>
  <li>4th tab has menu_4 (only for 1st child fragment)</li>
  <li>5th tab has menu_5</li>
  <li>Initially, there should be no menu for tab 1 which is ok. Then moving to 3rd tab directly it shows menu_4 which by default should show no menu. Then sliding to tab 4, it will show proper menu_4 then sliding back to 3rd tab it will show no menu (which is as required).</li>
  <li>This same thing happens for tab 5. If I am switching to 2nd child fragment within 2nd tab then the same behavior is observed with 1st tab.</li>
  </ul>
  
  <p>In short, according to my observation it is showing the menu of adjacent tabs which is actually getting executed after the current fragment and therefore such behavior is occurring.</p>
  
  <p>So how to avoid this?</p>