I saw many related questions about this topic, but none of them helped me. I am using a StaggeredGridLayout for my RecyclerView (showing cards). Every CardView opens a new activity, the problem I am facing is that on back press from that new activity, the RecyclerView scrolls back to the top. I want it to retain the previously scrolled position.
This what I use for now to set the layout :
rv.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
rv.setHasFixedSize(true);
Any proper working solution for a newbie?
EDIT :
@Override
protected void onResume() {
super.onResume();
if (listState != null) {
rvlayout.onRestoreInstanceState(listState);
}
initializeData();
}
Layout :
RecyclerView.LayoutManager rvlayout = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
and initializeData :
public void initializeData() {
rv.setLayoutManager(rvlayout);
rv.setHasFixedSize(true);
persons.clear();
c = db.rawQuery(SELECT_SQL, null);
c.moveToFirst();
if (!c.moveToFirst())
return;
while (!c.isLast()) {
id = c.getString(0);
names = c.getString(1);
age = c.getString(2);
persons.add(new Person(id,names,age));
c.moveToNext();
}
adapter = new RVAdapter(persons);
rv.setAdapter(adapter);
}