私には3つの活動があります: A --> B --> C
アクティビティ B では、RecyclerView の GridlayoutManager を使用してデータを入力しています。アクティビティ C に移動したときにスクロール状態を保存し、アクティビティ C からアクティビティ B に戻ったときにスクロール状態を復元したいと考えています。
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);
}
}
しかし、これは、アクティビティ B から戻るボタンを押してアクティビティ A に移動し、アクティビティ A からアクティビティ B に戻ると機能します。