Layoutmanager. Onsaveinstancestate() doesn't work. What's going on?

Meatball 2021-09-15 10:15:52

I think GridLayoutManager Save my RecyclerView The state of , But when you call onSaveInstanceState() Always return the same object when . I use the method "findFirstVisibleItemPosition" when , It always comes back -1.

RecyclerView With adapter , The adapter has some elements , I can roll them ! I don't know why I can't get the project location of fully initialized and working components ! Please help !

This is my code :

@Override
protected void onSaveInstanceState(Bundle outState) {

super.onSaveInstanceState(outState);
outState.putParcelable(BUNDLE_MOVIES_RECYCLER_VIEW_STATE, mMoviesRecyclerView.getLayoutManager().onSaveInstanceState());
outState.putInt(BUNDLE_SEGMENT_POSITION, mSelectedFilterPosition);
outState.putSerializable(BUNDLE_NOW_PLAYING_MOVIE, mNowPlayingMovie);
outState.putSerializable(BUNDLE_SEGMENTED_MOVIES, mSegmentMovies);

}
The solution refers to this post :https://www.itbaoku.cn/post/2...()-not-working
Solution
Problem solved ! To save the state of the layout manager , Need to be in the active onPause Call in method LayoutManager.onSaveInstanceState() Method . Because it's active LayoutManager Of onSaveInstanceState It has been cleared from the view , Or just delete the layout configuration . Code example :
public class MainActivity extends AppCompatActivity { RecyclerView recyclerView; private Parcelable mLayoutManagerState; private static final String LAYOUT_MANAGER_STATE = "LAYOUT_MANAGER_STATE"; @Override protected void onPause() { super.onPause(); mLayoutManagerState = recyclerView.getLayoutManager().onSaveInstanceState(); } @Override public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) { super.onCreate(savedInstanceState, persistentState); recyclerView = (RecyclerView) findViewById(R.id.recyclerview); if (savedInstanceState != null) { mLayoutManagerState = savedInstanceState.getParcelable(LAYOUT_MANAGER_STATE); recyclerView.getLayoutManager().onRestoreInstanceState(mLayoutManagerState); } } @Override public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) { super.onSaveInstanceState(outState, outPersistentState); outState.putParcelable(LAYOUT_MANAGER_STATE, mLayoutManagerState); }}

Please bring the original link to reprint ,thank
Similar articles

2021-09-15

2021-09-15

2021-09-15

2021-09-15