3

This is a conceptual issue. Sorry if it is a simple question, I start to learn Android few days ago.

I was trying to save the state of a recyclerview when the user out of the activity. And I read some articles about this.

In this article https://panavtec.me/retain-restore-recycler-view-scroll-position the methods are :

protected Parcelable onSaveInstanceState();
protected void onRestoreInstanceState(Parcelable state);

In this other article RecyclerView store / restore state between activities the methods are:

protected void onSaveInstanceState(Bundle state)
protected void onRestoreInstanceState(Bundle state)

These two articles pretend to answer the same question, how to restore the state of a recyclerview.


Questions:

1 - The first article implements these methods on a layoutManager!? So, I'm using a default GridLayoutManager, so to implement save and restore instance in the GridLayoutManager I should create my own class extending the default class?

2 - I can implement these methods in the layoutmanager regardless of implementing them in the activity?

3 - Where is the correct place to implement these methods? or is there a official answer to the question: "How restore the state of a recyclerview?"

I am looking for opinions on these three questions, not a full implementation.

romeuBraga
  • 2,135
  • 1
  • 8
  • 20

1 Answers1

2

1 - The first article implements these methods on a layoutManager!? So, I'm using a default GridLayoutManager, so to implement save and restore instance in the GridLayoutManager I should create my own class extending the default class?

If you look at both the article and the SO post they do not implement anything inside the LayoutManager, they just use methods that already exist. If you look in the documentation page for the GridLayoutManager there are already both a onSaveInstanceState() and a onRestoreInstanceState (Parcelable state) methods (these two methods are the "convenient API" the blog mentions at the start).

As I'm sure you've noticed GridLayoutManager inherits from LinearLayoutManager official documentation of LinearLayoutManager.onSaveInstanceState():

Called when the LayoutManager should save its state. [...] Returns: Parcelable Necessary information for LayoutManager to be able to restore its state

official documentation of LinearLayoutManager.onRestoreInstanceState (Parcelable state) Very incomplete but you can tell that it uses the same Parcelable parameter returned by LinearLayoutManager.onSaveInstanceState()

2 - I can implement these methods in the layoutmanager regardless of implementing them in the activity?

To be clear: no need to re-implement the LayoutManager. I don't see why you would need to do that, the methods are there and ready to use. The Activity methods of the same name are what you need to implement.

3 - Where is the correct place to implement these methods? or is there a official answer to the question: "How restore the state of a recyclerview?"

The correct place to do this is the Activity's lifecycle methods, these will be called at the appropriate times to save and restore your LayoutManager state. Quoting the SO answer you mentioned:

//---> this is the Activity's onSaveInstanceState
protected void onSaveInstanceState(Bundle state) {
     super.onSaveInstanceState(state);

     // Save list state
     mListState = mLayoutManager.onSaveInstanceState();
     state.putParcelable(LIST_STATE_KEY, mListState);
}

//---> this is the Activity's onRestoreInstanceState
protected void onRestoreInstanceState(Bundle state) {
    super.onRestoreInstanceState(state);

    // Retrieve list state and list/item positions
    if(state != null)
        mListState = state.getParcelable(LIST_STATE_KEY);
}

//---> this is the Activity's onResume
@Override
protected void onResume() {
    super.onResume();

    if (mListState != null) {
        mLayoutManager.onRestoreInstanceState(mListState);
    }
}

I haven't seen any official documentation about specifically restoring the RecyclerView/LayoutManager state but the lifecycle subject is a very important one in Android. I believe that after fully understanding this one can make the right decisions regarding specific use-cases.

Hope this helps ;)

HenriqueMS
  • 3,864
  • 2
  • 30
  • 39
  • ok, thanks for the answer, could you answer me, why I when try to use the signature "protected Parcelable onSaveInstanceState()" with @Override anotation (like the link in the question) I receive "Method does not override method from its superclass" (I'm trying to use this signature on a Activity). But when I use this signature "protected void onSaveInstanceState(Bundle state)" all works fine? – romeuBraga Apr 30 '18 at 01:06
  • @romeuBraga yeah I think you might be confusing the two signatures (which is understandable since the methods have the same name) - the correct Activity signature is the one here: https://developer.android.com/reference/android/app/Activity.html#onSaveInstanceState(android.os.Bundle) The first one doesn't work because You are trying to override the LinearLayoutManager.onSaveInstanceState() method - in other words this method does not exist in the Activity class ;) – HenriqueMS Apr 30 '18 at 01:21