I have created a simple recyler view in an activity (say, A1) where user can add/delete the items in the reclycler view. I now am trying to save it while navigating to second activity (say, A2) and restore it while moving back (A2 -> A1), but am unable to do so. While coming back to A1 from A2, the recycler view is initialized again, losing all the user modified data. Requesting your help with the same.
I have tried the onSaveInstanceState and onRestoreInstanceState methods, but when I navigate to A1, the onRestoreInstanceState is not being called. I can't seem to figure this out. Any help would be appreciated.
My code:
//rv is the recycler view I am trying to save
//STATE_KEY and TAG are strings for Log statement
Parcelable rv_state;
protected void onSaveInstanceState(Bundle state) {
super.onSaveInstanceState(state);
rv_state = rv.getLayoutManager().onSaveInstanceState();
state.putParcelable(STATE_KEY, rv_state);
Log.d(TAG, " on save rvstate setup = " + rv_state); //rv_state not null here
}
protected void onRestoreInstanceState(Bundle state) { //never called
super.onRestoreInstanceState(state);
Log.d(TAG, "b4 onRestoreinstance state check " + state);
if (state != null)
Log.d(TAG, "onRestoreinstance in if state check " + state);
rv_state = state.getParcelable(STATE_KEY);
}
@Override
protected void onResume() {
super.onResume();
Log.d(TAG, "b4 onResume rv_state check " + rv_state); //rv_state is always null
if (rv_state != null) {
Log.d(TAG, "in onResume rv_state check");
rv.getLayoutManager().onRestoreInstanceState(rv_state);
}
}