3

I have 3 activities: A --> B --> C

In Activity B, I'm populating using RecyclerView's GridlayoutManager. I want to save the scrolled state when i navigate to Activity C and restore the scrolled state when i go back to Activity B from Activity C.

private RecyclerView mImgList;
private GridLayoutManager mRecyclerGridMan;
private final String KEY_RECYCLER_STATE = "recycler_state";
private Parcelable mListState = null;
private static Bundle mBundleRecyclerViewState;

.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

.

@Override
protected void onPause()
{
    super.onPause();
    mBundleRecyclerViewState = new Bundle();
    mListState = mImgList.getLayoutManager().onSaveInstanceState();
    mBundleRecyclerViewState.putParcelable(KEY_RECYCLER_STATE, mListState);
}

.

@Override
protected void onResume()
{
    super.onResume();
    if (mBundleRecyclerViewState != null) {
        mListState = mBundleRecyclerViewState.getParcelable(KEY_RECYCLER_STATE);
        mImgList.getLayoutManager().onRestoreInstanceState(mListState);
    }
}

But this works when I press back button from Activity B and go to Activity A, and navigating back to Activity B from Activity A.

Zayid Mohammed
  • 2,275
  • 2
  • 10
  • 17

1 Answers1

0

Restore state in the onRestoreInstanceState()

protected void onRestoreInstanceState(Bundle state) {
super.onRestoreInstanceState(state);

// Retrieve list state and list/item positions
if(state != null)
   mListState =   mBundleRecyclerViewState.getParcelable(KEY_RECYCLER_STATE);
}
sasikumar
  • 12,540
  • 3
  • 28
  • 48
  • I tried, but not working. Even the scroll state is not restored when navigating to Activity A to B, which was working before. – Zayid Mohammed Nov 14 '16 at 08:33
  • Here's the link to complete code: https://github.com/zayid/Blur_Wallpaper/blob/master/app/src/main/java/com/whackyard/whatsappwalls/MainActivity.java – Zayid Mohammed Nov 14 '16 at 10:05