0

I found some code to save and restore the state of RecyclerView after screen rotation, but it's not working.

Code:

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putParcelable(Constants.LIST_SAVED_POSITION, recyclerView.getLayoutManager().onSaveInstanceState());
}

@Override
public void onViewStateRestored(Bundle savedInstanceState) {
    super.onViewStateRestored(savedInstanceState);
    if (savedInstanceState!=null && savedInstanceState.containsKey(Constants.LIST_SAVED_POSITION)){
        Parcelable parcelable;
        parcelable = savedInstanceState.getParcelable(Constants.LIST_SAVED_POSITION);
        recyclerView.getLayoutManager().onRestoreInstanceState(parcelable);
    }
}

The old position is not restored after a screen rotation. I know that I can save integer variables, but this way seems better.

Can someone help me solve this using this approach.

kandroidj
  • 13,784
  • 5
  • 64
  • 76
Mansur Nashaev
  • 293
  • 1
  • 5
  • 15
  • Check this post, it seems works http://stackoverflow.com/questions/28236390/recyclerview-store-restore-state-between-activities – AndroidRuntimeException Feb 16 '16 at 16:17
  • where do you have this code? In a fragment or did you extend RecyclerView and drop this code in? – kandroidj Feb 16 '16 at 16:25
  • @inner_class7 In a fragment – Mansur Nashaev Feb 16 '16 at 16:45
  • have you tried just debugging to see if parcelable is coming back as not null? – kandroidj Feb 16 '16 at 16:50
  • yes. it's successfully coming back. – Mansur Nashaev Feb 16 '16 at 16:54
  • @inner_class7 Looks good but it's amazing that almost every article on RecyclerViews has its focus on creating a list, but none on saving the list. It does no good to create a beautifully crafted/valuable list (say a big list of songs I want to download) and then have the list literally be destroyed if I navigate away from the list with the single press of the back button. Why wouldn't saving state code be a requirement for creating every RecyclerView list just like the other requirements: the adapter, the layoutmanager, onCreateViewHolder, onBindViewHolder and getItemCount? Thoughts? – AJW Feb 17 '16 at 03:37

3 Answers3

0

you can add this to your activity in the manifist.xml to disable the activity to recreate itself ,So after screen rotation the activity save all variables.

    android:configChanges="orientation|keyboardHidden" 
     >
user2934536
  • 149
  • 8
0

I found the problem. This code is being called on the fragment. But the problem was in the re-creation of the fragment in the activity. When I fixed recreating the fragment, list restores the position even without saving it state.

old code:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_supplication_detail);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    chapter = getIntent().getIntExtra(Constants.SELECTED_ITEM, 1);

    fragmentSupplicationDetail = new FragmentSupplicationDetail();
    Bundle extras = new Bundle();
    extras.putInt(Constants.SELECTED_ITEM, chapter);
    fragmentSupplicationDetail.setArguments(extras);
    transaction.replace(R.id.container, fragmentSupplicationDetail, SUPPLICATION_FRAGMENT_TAG).commit();
}

fixes:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_supplication_detail);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    chapter = getIntent().getIntExtra(Constants.SELECTED_ITEM, 1);
    if (getSupportFragmentManager().findFragmentByTag(SUPPLICATION_FRAGMENT_TAG) == null){
        fragmentSupplicationDetail = new FragmentSupplicationDetail();
        Bundle extras = new Bundle();
        extras.putInt(Constants.SELECTED_ITEM, chapter);
        fragmentSupplicationDetail.setArguments(extras);
        Log.d(Constants.LOG_TAG, getClass().getSimpleName() + ": Создаем фрагмент ");
    }else {
        fragmentSupplicationDetail = (FragmentSupplicationDetail) getSupportFragmentManager().findFragmentByTag(SUPPLICATION_FRAGMENT_TAG);
        Log.d(Constants.LOG_TAG, getClass().getSimpleName() + ": Восстанавливаем фрагмент ");
    }
    transaction.replace(R.id.container, fragmentSupplicationDetail, SUPPLICATION_FRAGMENT_TAG).commit();
}
Mansur Nashaev
  • 293
  • 1
  • 5
  • 15
-1

I was recently working with recycler view and wanted to have the same functionality as you want, I have followed the below mentioned tutorial,even after rotating the screen It maintains the state of the view.Please refer to the following link.

http://www.androidtutorialpoint.com/material-design/listing-items-using-recyclerview/

Hope This helps !!

Kapil Gupta
  • 332
  • 2
  • 8