title: Cannot save instance state for recyclerview in fragment when orientation change
body: <p>I'm having trouble getting my recyclerview to save state on orientation change. Have used onSaveInstanceState in my fragment but obviously not correctly as it is not working. Any advice and pointers on how to fix? </p>
  
  <p>SecondActivity</p>
  
  <pre><code>public class SecondActivity extends AppCompatActivity {
  
  
  private boolean mTwoPane;
  ArrayList&lt;Recipe&gt; myList;
  List&lt;Steps&gt; steps;
  android.support.v7.app.ActionBar actionBar;
  StepsFragment fragment;
  
  //Eventbus
  @Override
  protected void onStart() {
      super.onStart();
      EventBus.getDefault().register(this);
  }
  
  
  @Override
  protected void onStop() {
      super.onStop();
      EventBus.getDefault().unregister(this);
  }
  
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main_two);
  
  
      actionBar = getSupportActionBar();
      getSupportActionBar().setDisplayHomeAsUpEnabled(true);
      getSupportActionBar().setHomeButtonEnabled(true);
  
  
      myList = getIntent().getParcelableArrayListExtra("list");
      String id = getIntent().getStringExtra("intent");
      int result = Integer.parseInt(id);
      steps = (ArrayList) myList.get(result - 1).getSteps();
  
      String nameString = myList.get(result - 1).getName();
  
      getSupportActionBar().setTitle(nameString);
  
  
      if (findViewById(R.id.android_me_linear_layout) != null) {
  
          mTwoPane = true;
  
          if (savedInstanceState == null) {
  
              Bundle arguments = new Bundle();
  
  
              arguments.putParcelableArrayList("myList", myList);
              arguments.putInt("result", result);
              arguments.putBoolean("twoPane", mTwoPane);
  
              StepsFragment fragment = new StepsFragment();
  
              fragment.setArguments(arguments);
  
              getSupportFragmentManager().beginTransaction()
                      .add(R.id.fragment_container, fragment)
                      .commit();
  
  
          }
  
  
  
      } else {
          // We're in single-pane mode and displaying fragments on a phone in separate activities
          mTwoPane = false;
  
          Bundle arguments = new Bundle();
  
  
          arguments.putParcelableArrayList("myList", myList);
          arguments.putInt("result", result);
          arguments.putBoolean("twoPane", mTwoPane);
  
          StepsFragment fragment = new StepsFragment();
  
          fragment.setArguments(arguments);
  
          getSupportFragmentManager().beginTransaction()
                  .add(R.id.fragment_container, fragment)
                  .commit();
  
  
  
      }
  
  
  }
  
  
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
      switch (item.getItemId()) {
          case android.R.id.home:
              onBackPressed();
              return true;
      }
      return super.onOptionsItemSelected(item);
  }
  
  @Override
  public void onBackPressed() {
      super.onBackPressed();
      if(mTwoPane) {
  
  
          startActivity(new Intent(SecondActivity.this, MainActivity.class));
          finish();
      }
  
  }
  
  @Subscribe(threadMode = ThreadMode.MAIN)
  public void onMessage(Message event) {
  
      if (event.getMessage() == 2) {
  
  
          Fragment newFragment = new DetailFragment();
  
          newFragment.setArguments(event.getBundle());
  
          FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
  
          transaction.replace(R.id.detail_container, newFragment);
  
  
          transaction.addToBackStack(null);
  
          transaction.commit();
  
          Log.d(LOG_TAG, "Eventbus worked");
  
  
      }
  
      else if ( event.getMessage() == 3){
  
          Fragment newFragment = new DetailFragment();
  
          newFragment.setArguments(event.getBundle());
  
          FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
  
          transaction.replace(R.id.fragment_container, newFragment);
  
          transaction.addToBackStack(null);
  
          transaction.commit();
  
  
  
      }
  
  
  }
  
  }
  </code></pre>
  
  <p>StepsFragment</p>
  
  <pre><code>public class StepsFragment extends android.support.v4.app.Fragment {
  
  
  
  ArrayList&lt;Recipe&gt; myList;
  List&lt;Steps&gt; myStepsList;
  List &lt;Ingredients&gt; ingredients;
  
  private RecyclerView recyclerView;
  StepItemRecyclerViewAdapter stepItemRecyclerViewAdapter;
  private RecyclerView.LayoutManager mLayoutManager;
  
  int result;
  List&lt;String&gt; ingredientsString;
  String string;
  Boolean mTwoPane;
  Parcelable listState;
  
  @Override
  public void onSaveInstanceState(Bundle outState) {
      super.onSaveInstanceState(outState);
      outState.putParcelableArrayList("myArrayListTwo",(ArrayList) myStepsList);
  }
  
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
  
  
      if(savedInstanceState == null || !savedInstanceState.containsKey("myArrayListTwo")){
          myStepsList = new ArrayList&lt;&gt;();
      }
  
      else{
          myStepsList = savedInstanceState.getParcelableArrayList("myArrayListTwo");
      }
  
  
      mTwoPane = getArguments().getBoolean("twoPane");
      myList = getArguments().getParcelableArrayList("myList");
      result = getArguments().getInt("result") - 1;
      myStepsList = myList.get(result).getSteps();
      ingredients = myList.get(result).getIngredients();
  
  
      //Create a String array for all ingredients strings
      ingredientsString = new ArrayList&lt;&gt;();
      ingredientsString.add("Ingredients" + "\n");
  
      for(int x = 0; x &lt; ingredients.size(); x++){
  
          ingredientsString.add(ingredients.get(x).getIngredient() + " " + ingredients.get(x).getQuantity() + " " + ingredients.get(x).getMeasure());
          Log.d(LOG_TAG, ingredientsString.get(x).toString());
      }
  
  
  
      StringBuilder stringBuilder = new StringBuilder();
      for(String s : ingredientsString){
          stringBuilder.append(s + "\n");
      }
  
      string = stringBuilder.toString();
  
  
  
  }
  
  
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  
      View rootView = inflater.inflate(R.layout.fragment_steps, container, false);
  
          recyclerView = (RecyclerView) rootView
                  .findViewById(R.id.my_recycler_view);
  
          mLayoutManager = new LinearLayoutManager(getActivity());
  
          recyclerView.setLayoutManager(mLayoutManager);
          stepItemRecyclerViewAdapter = new StepItemRecyclerViewAdapter(myStepsList);
          recyclerView.setAdapter(stepItemRecyclerViewAdapter);
  
  
          TextView textView = (TextView) rootView.findViewById(R.id.text_view);
          textView.setText(string);
  
  
      return rootView;
  }
  
  
  public class StepItemRecyclerViewAdapter
          extends RecyclerView.Adapter&lt;StepItemRecyclerViewAdapter.ViewHolder&gt; {
  
          private final List&lt;Steps&gt; mValues;
  
  
  
      public StepItemRecyclerViewAdapter(List&lt;Steps&gt; items) {
  
          mValues = items;
      }
  
      @Override
      public StepItemRecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
          View view = LayoutInflater.from(parent.getContext())
                  .inflate(R.layout.item_list_content, parent, false);
          return new StepItemRecyclerViewAdapter.ViewHolder(view);
      }
  
      @Override
      public void onBindViewHolder(final StepItemRecyclerViewAdapter.ViewHolder holder, int position) {
  
  
          holder.mItem = mValues.get(position);
  
  
          holder.mContentView.setText(mValues.get(position).getShortDescription());
  
  
  
  
          holder.mView.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
  
  
                  if(mTwoPane){
                      Bundle args = new Bundle();
  
                      args.putParcelableArrayList("myList", myList);
                      args.putInt("result", result);
                      args.putString("int", holder.mItem.getId());
                      EventBus.getDefault().postSticky(new Message(2, args));
                      Log.d(LOG_TAG, "You clicked a button" + holder.mItem.toString());
  
  
  
                  } else if( mTwoPane == false){
  
                      Bundle args = new Bundle();
  
                      args.putParcelableArrayList("myList", myList);
                      args.putInt("result", result);
                      args.putString("int", holder.mItem.getId());
                      EventBus.getDefault().postSticky(new Message(3, args));
                      Log.d(LOG_TAG, "You clicked a button" + holder.mItem.toString());
  
                  }
  
  
              }
          });
      }
  
      @Override
      public int getItemCount() {
          return mValues.size();
      }
  
      public class ViewHolder extends RecyclerView.ViewHolder {
          public  View mView;
          public TextView mContentView;
          public Steps mItem;
  
  
          public ViewHolder(View view) {
              super(view);
              mView = view;
              mContentView = (TextView) view.findViewById(R.id.content);
  
          }
  
          @Override
          public String toString() {
              return super.toString() + " '" + mContentView.getText() + "'";
          }
      }
  }
  
  
  
  
  
  
  
  
  
  
  
  
  
  }
  </code></pre>
  
  <p>activity_main_two.xml
  <a href="https://github.com/RJRogers/TwoPaneUITest/blob/master/app/src/main/res/layout/activity_main_two.xml" rel="nofollow noreferrer">https://github.com/RJRogers/TwoPaneUITest/blob/master/app/src/main/res/layout/activity_main_two.xml</a> </p>
  
  <p>fragment_steps.xml
  <a href="https://github.com/RJRogers/TwoPaneUITest/blob/master/app/src/main/res/layout/fragment_steps.xml" rel="nofollow noreferrer">https://github.com/RJRogers/TwoPaneUITest/blob/master/app/src/main/res/layout/fragment_steps.xml</a></p>