title: onOptionsItemSelected of Fragments in FragmentStatePagerAdapter holds previous this reference
body: <p>I have fragment (in FragmentStatePagerAdapter) which has global variable, and in onOptionsItemSelected I refer this value. It works fine for the first time, when I go back and come to the same page and update this global variable and now if I refer the values in onOptionsItemSelected it holds the previous values and not the current one.
  I tried to print the "this" reference in onOptionsItemSelected, it is referring to the first reference.  Whereas the values in other methods refers to the updated value.</p>
  
  <p>Edit 1 : Adding code snippet</p>
  
  <pre><code>   public class CustomizeFieldFragment extends BaseFragment implements CustomizeFieldListAdapter.IOnSelectCustomizeField {
  
  private MenuItem mMenuItem;
  private List&lt;String&gt; mSelectedFieldsId;
  private boolean mShowDoneMenu;
  
  @Override
  public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
      menu.clear();
      inflater.inflate(R.menu.customize_field_done, menu);
      mMenuItem = menu.findItem(R.id.action_done);
      mMenuItem.setVisible(false);
      super.onCreateOptionsMenu(menu, inflater);
  }
  
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
      switch (item.getItemId()) {
          case R.id.action_done:
              updateSelectedFields();
              return true;
          default:
              return super.onOptionsItemSelected(item);
      }
  }
  
  @Override
  public void onSelect(List&lt;String&gt; selectedFieldsId) {
     if(!selectedFieldsId.isEmpty()){
          mSelectedFieldsId = new ArrayList&lt;&gt;(selectedFieldsId);
          mShowDoneMenu = true;
      }
  }
  
  private void updateSelectedFields() {
  
      String[] selectionArgs = mSelectedFieldsId.toArray(new String[mSelectedFieldsId.size()]);
      Uri uri = Contract.DefectFieldEntry.buildDefectFieldUri(baseUrl, domainName, projectName);
      ContentValues cv = new ContentValues();
      cv.put(Contract.DefectFieldEntry.COLUMN_IS_FIELD_SELECTED, true);
   //  mContext.getContentResolver().update(uri, cv, null, selectionArgs);
  }
  }
  </code></pre>
  
  <p>The issue is with "mSelectedFieldsId" variable. When I refer this in onOptionsItemSelected it refers to old values that was stored and in other methods it refers to the actual value.</p>
  
  <p>Edit 2 : Adapter code snippet</p>
  
  <pre><code>public class CustomizeFieldListAdapter  extends RecyclerView.Adapter&lt;CustomizeFieldListAdapter.CustomizeFieldListViewHolder&gt; {
  
  private static final String TAG = CustomizeFieldListAdapter.class.getSimpleName();
  private Cursor mCursor;
  private SparseBooleanArray mSelectedItems;
  private IOnSelectCustomizeField onSelectCustomizeField;
  private List&lt;String&gt; mSelectedItemsId;
  
  public CustomizeFieldListAdapter(IOnSelectCustomizeField iOnSelectCustomizeField){
      onSelectCustomizeField = iOnSelectCustomizeField;
      mSelectedItems = new SparseBooleanArray();
      mSelectedItemsId = new ArrayList&lt;&gt;();
  }
  
  public class CustomizeFieldListViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
  
      TextView tvFieldName;
      ImageView selectionIcon;
  
      public CustomizeFieldListViewHolder(View itemView) {
          super(itemView);
          tvFieldName = (TextView) itemView.findViewById(R.id.customize_field_name);
          selectionIcon = (ImageView) itemView.findViewById(R.id.selection_icon);
          itemView.setOnClickListener(this);
      }
  
      @Override
      public void onClick(View view) {
          int adapterPos = getAdapterPosition();
          toggleSelection(adapterPos);
      }
  }
  
  @Override
  public CustomizeFieldListViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
      View rootView = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_customize_field,
              parent, false);
      return new CustomizeFieldListViewHolder(rootView);
  }
  
  @Override
  public void onBindViewHolder(CustomizeFieldListViewHolder holder, int position) {
      mCursor.moveToPosition(position);
      holder.tvFieldName.setText(mCursor.getString(1));
      if(mSelectedItems.get(position, false))
          holder.selectionIcon.setImageResource(R.mipmap.select);
      else
          holder.selectionIcon.setImageResource(R.mipmap.selection_icon);
  }
  
  @Override
  public int getItemCount() {
      return mCursor == null ? 0 : mCursor.getCount();
  }
  
  public void swapCursor(Cursor cursor){
      mCursor = cursor;
      notifyDataSetChanged();
  }
  
  private void toggleSelection(int position){
      mCursor.moveToPosition(position);
      if(mSelectedItems.get(position, false)){
          mSelectedItems.delete(position);
          mSelectedItemsId.remove(mCursor.getString(0));
      }
      else{
          mSelectedItems.put(position, true);
          mSelectedItemsId.add(mCursor.getString(0));
      }
      onSelectCustomizeField.onSelect(mSelectedItemsId);
      notifyItemChanged(position);
  }
  
  public interface IOnSelectCustomizeField {
      void onSelect(List&lt;String&gt; selectedItemsId);
  }
  </code></pre>
  
  <p>}</p>