title: Maintaining list items positions on device rotation in an android fragment
body: <p>In a fragment I am trying to save the scroll state of the RecyclerView list, but somehow it is not saving the state. As it is a fragment, I am overriding the onSaveInstanceState() and onActivityCreated() methods to save the scroll position. Even tried implementing in onViewStateRestored() method. I saw related some posts on saving the scroll state but it ain't working. Kindly let me know where am I failing. Below is my code:</p>
  
  <pre><code>public class RecipeListFragment extends Fragment
      implements RecipeListContract.View {
  @BindView(R.id.recipe_list_recycler_view)
  RecyclerView mRecipeListRecyclerView;
  @BindView(R.id.recipe_list_progress_bar)
  ProgressBar mRecipeListProgressBar;
  
  @BindInt(R.integer.grid_column_count)
  int mGridColumnCount;
  @BindString(R.string.recipe_list_sync_completed)
  String mRecipeListSyncCompleted;
  @BindString(R.string.recipe_list_connection_error)
  String mRecipeListConnectionError;
  
  GridLayoutManager gridLayoutManager;
  Parcelable savedRecyclerLayoutState;
  
  Unbinder unbinder;
  
  private static final String SAVED_LAYOUT_MANAGER
          = "com.learnwithme.buildapps.bakingapp.ui.recipelist.fragment";
  
  private RecipeListContract.Presenter mRecipeListPresenter;
  private RecipeListAdapter mRecipeListAdapter;
  
  public RecipeListFragment() { }
  
  @Nullable
  @Override
  public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup 
  container, @Nullable Bundle savedInstanceState) {
      View view = inflater.inflate(R.layout.fragment_recipe_list, container, 
  false);
  unbinder = ButterKnife.bind(this, view);
  
  mRecipeListAdapter = new RecipeListAdapter(
              getContext(),
              new ArrayList&lt;&gt;(0),
              recipeId -&gt; mRecipeListPresenter.loadRecipeDetails(recipeId)
  );
  mRecipeListAdapter.setHasStableIds(true);
  
      gridLayoutManager = new GridLayoutManager(getContext(), 
  mGridColumnCount);
      mRecipeListRecyclerView.setLayoutManager(gridLayoutManager);
      mRecipeListRecyclerView.setHasFixedSize(true);
      mRecipeListRecyclerView.setAdapter(mRecipeListAdapter);
  
      return view;
  }
  
  @Override
  public void onPause() {
      super.onPause();
      mRecipeListPresenter.unsubscribe();
  }
  
  @Override
  public void onResume() {
      super.onResume();
      mRecipeListPresenter.subscribe();
  }
  
  @Override
  public void onSaveInstanceState() { }
  
  @Override
  public void onSaveInstanceState(Bundle bundle) {
      super.onSaveInstanceState(bundle);
      if(bundle != null) {
          bundle.putParcelable(SAVED_LAYOUT_MANAGER,
                  mRecipeListRecyclerView
                      .getLayoutManager()
                      .onSaveInstanceState());
          Timber.d("instance state=&gt;", 
  mRecipeListRecyclerView.getLayoutManager().onSaveInstanceState());
      }
  }
  
  @Override
  public void onViewStateRestored(@Nullable Bundle bundle) {
      super.onViewStateRestored(bundle);
      if(bundle != null) {
          savedRecyclerLayoutState = 
  bundle.getParcelable(SAVED_LAYOUT_MANAGER);
          Timber.d("onViewStateRestored savedRecyclerLayoutState=&gt;", 
  savedRecyclerLayoutState);
          mRecipeListRecyclerView
                  .getLayoutManager()
                  .onRestoreInstanceState(savedRecyclerLayoutState);
      }
  }
  
  @Override
  public void onActivityCreated(@Nullable Bundle bundle) {
      super.onActivityCreated(bundle);
      if(bundle != null) {
          savedRecyclerLayoutState = 
  bundle.getParcelable(SAVED_LAYOUT_MANAGER);
          Timber.d("onViewStateRestored savedRecyclerLayoutState=&gt;",
                  savedRecyclerLayoutState);
          mRecipeListRecyclerView
                  .getLayoutManager()
                  .onRestoreInstanceState(savedRecyclerLayoutState);
      }
  }
  
  @Override
  public void onDestroyView() {
      super.onDestroyView();
      unbinder.unbind();
  }
  
  public static RecipeListFragment newInstance() {
      return new RecipeListFragment();
  }
  
  @Override
  public void setPresenter(RecipeListContract.Presenter recipeListPresenter) {
      this.mRecipeListPresenter = recipeListPresenter;
  }
  
  @Override
  public void showRecipeList(List&lt;Recipe&gt; recipeList) {
      mRecipeListAdapter.refreshRecipes(recipeList);
  }
  
  @Override
  public void loadProgressBar(boolean show) {
      setViewVisibility(mRecipeListRecyclerView, !show);
      setViewVisibility(mRecipeListProgressBar, show);
  }
  
  @Override
  public void displayCompletedMessage() {
      Toast.makeText(getContext(), mRecipeListSyncCompleted, 
  Toast.LENGTH_SHORT).show();
  }
  
  @Override
  public void displayErrorMessage() {
      Toast.makeText(getContext(), mRecipeListConnectionError, 
  Toast.LENGTH_SHORT).show();
  }
  
  @Override
  public void displayRecipeDetails(int recipeId) {
      startActivity(RecipeDetailsActivity.prepareIntent(getContext(), 
  recipeId));
  }
  
  private void setViewVisibility(View view, boolean visible) {
      if (visible) {
          view.setVisibility(View.VISIBLE);
      } else {
          view.setVisibility(View.INVISIBLE);
      }
  }
  }
  </code></pre>