@Override
 public boolean onOptionsItemSelected(MenuItem item) {
   if (mSettingsMenu.onOptionsItemSelected(item)) {
     invalidateOptionsMenu();
     return true;
   } else {
     return super.onOptionsItemSelected(item);
   }
 }
  @Override
  protected void onSaveInstanceState(Bundle outState) {
    mPagerPhotoPosition =
        mAnimator.isLeaving() || mPagerAdapter.getCount() == 0 ? -1 : mViews.pager.getCurrentItem();

    if (mViews.grid.getChildCount() > 0) {
      View child = mViews.grid.getChildAt(0);
      mGridPosition = mViews.grid.getChildAdapterPosition(child);
      mGridPositionFromTop =
          child.getTop() - Views.getMarginParams(child).topMargin - mViews.grid.getPaddingTop();
    } else {
      mGridPosition = -1;
      mGridPositionFromTop = 0;
    }

    mSettingsMenu.onSaveInstanceState(outState);
    super.onSaveInstanceState(outState);
  }
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_advanced_demo);
    mViews = new ViewHolder(this);

    setSupportActionBar(mViews.toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    initDecorMargins();
    initGrid();
    initPager();
    initAnimator();

    mSettingsMenu = new GestureSettingsMenu();
    mSettingsMenu.onRestoreInstanceState(savedInstanceState);

    if (mPagerPhotoPosition != -1) {
      // Photo was show in pager, we should switch to pager mode instantly
      onPositionUpdate(1f, false);
    }
  }
 @Override
 public void onSetupGestureView(GestureView view) {
   mSettingsMenu.applySettings(view);
 }
 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
   return mSettingsMenu.onCreateOptionsMenu(menu);
 }