0

I am developing one android application my problem structure is like this.

One activity contains one fragment and this fragment contains one RecyclerView. RecyclerView item contains two textviews which displays simple text like this :

 TextTitle TextValue --> item1
 TextTitle TextValue --> item2
 TextTitle TextValue --> item3

If User taps on text value, its value gets changed (Value is binary so it will be only two). If user change first two item's value by tapping and I want to restore those two values when I will come back to this fragment with a help of restore/save state.

I followed many articles and gather information that if I have to use proper unique id in item's view layout for both textviews then recyclerview state will be stored automatically, only just you need to save whole recyclerview state. Its not working in my case. What am I doing wrong? Do I need to fill adapter again?

any help would be appreciated.

  @Override
  public void onSaveInstanceState(@NonNull Bundle outState) {
    super.onSaveInstanceState(outState);

    outState.putParcelable(INSTANCE_STATE_CONTENT_RECYCLER_VIEW,recyclerView.getLayoutManager().onSaveInstanceState());
  }

      // again restore it restore it
      if (recyclerViewState != null) {
        recyclerView.getLayoutManager().onRestoreInstanceState(recyclerViewState);
        recyclerViewState = null;
       }

      @Override
      public void onViewBound(@Nullable Bundle savedInstanceState) {
        //super.onViewBound(savedInstanceState);
        if (savedInstanceState != null) {
          recyclerViewState = savedInstanceState.getParcelable(INSTANCE_STATE_CONTENT_RECYCLER_VIEW);
        }
        // continue with my work.
      }
sam_k
  • 5,983
  • 14
  • 76
  • 110
  • `TextViews` are not saved automatically even with a unique ID like ` EditText`. See http://stackoverflow.com/questions/5179686/restoring-state-of-textview-after-screen-rotation. – Cheticamp Jun 24 '16 at 11:53
  • @Cheticamp thanks for reply, It does save scroll position? – sam_k Jun 24 '16 at 21:08
  • Yes. I believe RecyclerView does. – Cheticamp Jun 24 '16 at 21:10
  • @Cheticamp : it's not working in my case. I don't know why. – sam_k Jun 24 '16 at 21:15
  • My RecyclerViews just save their scroll position without any effort on my part. It seems that RecyclerView is a work in progress, so this all may be related to SDK version. I am on SDK 23. I did a little search and came across [this](https://gist.github.com/FrantisekGazo/a9cc4e18cee42199a287). I haven't tried it, but it may point you in the right direction. – Cheticamp Jun 25 '16 at 00:11

0 Answers0