0

I am trying to save my Recyclerview's LinearLayoutManager state.

Fragment.java :

@Override
    public void onSaveInstanceState(Bundle outState) {
        Log.i("frag", "onSaveInstanceState called");
        super.onSaveInstanceState(outState);
        outState.putParcelable("myState", mGridLayoutManager.onSaveInstanceState());
    }

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
          if (savedInstanceState != null) {
                Log.i(MainActivity.TAG, "restore called from createView");
                mGridLayoutManager.onRestoreInstanceState(savedInstanceState.getParcelable("myState"));
            }
          .....
          ....
    }

But savedInstanceState is always null.

I already tried to restore its state in the onCreate & onActivityCreated methods, but in vain. What am I doing wrong ?

I checked these questions to be certain that this is the correct way to save & restore :

Recyclerview store / save state

Save recyclerview scroll position

Community
  • 1
  • 1
Mohammed Aouf Zouag
  • 17,042
  • 4
  • 41
  • 67
  • read this [how to fragment save states](http://stackoverflow.com/questions/6787071/android-fragment-how-to-save-states-of-views-in-a-fragment-when-another-fragmen) I give up using save states of fragment :( – myoungjin Oct 29 '15 at 00:56
  • I did the exact same thing but it didn't work... I started to think that it's because I'm using the support library – Mohammed Aouf Zouag Oct 29 '15 at 08:22

1 Answers1

0

try putting your call to super.onSaveInstanceState(outstate) after you call outstate.putParcelable()?

If that doesn't work, maybe try just putting a string in the bundle and see if it has something to do with your mGridLayoutManager.onSaveInstanceState() failing?

Chad
  • 96
  • 4
  • did you move the call to super after you modified the outState? – Chad Oct 28 '15 at 23:26
  • well i just checked some of my code and my calls to super() are like yours, so nevermind on that. – Chad Oct 28 '15 at 23:29
  • I catch it in onCreate(Bundle) – Chad Oct 28 '15 at 23:34
  • wonder if you could be passing a different bundle to this fragment when you navigate to it and maybe it clashes with the one created in onSaveInstanceState()? – Chad Oct 28 '15 at 23:35
  • I don't think so because I use a static method to instantiate the fragment. Could it be a 'Fragment' or 'app.v4.Fragment' issue ? – Mohammed Aouf Zouag Oct 28 '15 at 23:38
  • sorry man idk, you should try making a new default project in android studio and have it instantiate a fragment for you. and then test the onSaveInstanceState() and onCreate() and see if it work there, then slowly move it towards your implementation till you find the bug. – Chad Oct 29 '15 at 01:07
  • I shall give it a try. Thanks – Mohammed Aouf Zouag Oct 29 '15 at 08:21