How to Save Scroll Position of RecylerView Activity when we go to next Detail Activity by using itemclicklistener from list of items(these items are retrieved from Firebase JSON) in RecyclerView and restore position when we come back to Recylerview Activity and i am Using LinearLayoutManager

Please Help Me Thanks in Advance

4

There are 4 best solutions below

1
On BEST ANSWER

I am not really clear with your question. I assumed you current activity was destroyed when you go to next detail, thus recreating the activity when you go back. Do not call finish when you go to next detail and just call onBackPressed to go back to your previous activity. This will maintain your recyclerView state and position.

Anyway, you can use Shared Preferences to save the position before go to next detail and get it back on onResume.

1
On

save state on Activity on onPause method and restore it on onResume() method See the code

 public class Activity extends AppCompatActivity
 {
    private final String STATE = "recycler_state";
    private RecyclerView mRecyclerView;
    private static Bundle mBundle;

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

        mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);//set to whatever view id you use
        // don't forget to set your adapter
    }

    @Override
    protected void onPause()
    {
        super.onPause();

        // save RecyclerView state

        mBundle = new Bundle();
        Parcelable listState = mRecyclerView.getLayoutManager().onSaveInstanceState();
        mBundle.putParcelable(STATE, listState);
    }

    @Override
    protected void onResume()
    {
        super.onResume();

        // restore RecyclerView state
        if (mBundle != null) {
            Parcelable listState = mBundle.getParcelable(STATE);
            mRecyclerView.getLayoutManager().onRestoreInstanceState(listState);
        }
    }
}
0
On

You can use this trick:

int firstItemPos = mLayoutManager.findFirstVisibleItemPosition();
View v = mLayoutManager.findViewByPosition(firstItemPos);
int offsetPixel = 0;
if(v != null) offsetPixel = v.getTop();

And then when you restore position:

mLayoutManager.scrollToPositionWithOffset(firstItemPos, offsetPixel)

firstItemPos and offsetPixel you can save in extra bundle of activity. mLayoutManager in my example I use ordinary LinearLayoutManager and set it to my Recycle View:

mLayoutManager = new LinearLayoutManager(getActivity())
1
On

Problem statement is unclear. Still I am trying to answer.

Assuming you starting new Activity on click of list. As per the Lifecycle of activity background activity will stay in the same state with scroll. When you finish new activity back activity will on same state.

This might happening due to below reasons

  1. You reloading data on onResume method. You should load data on onCreateView.
  2. You are starting background activity from second activity.