0

This is My file where I want to fetch position on screen rotation. and my saveInstance state But I am Facing a logical error while the screen is rotated I am getting a position for first but addOnScrollListener making it 0 and automatically called.

if(savedInstanceState != null) {
            position = savedInstanceState.getInt("Position");
            recyclerView.scrollToPosition(position);
        }

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView,newState);
                position = linearLayoutManager.findFirstVisibleItemPosition();
            }
        });
 
    @Override
    public void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt("Position", position);
    }
  • Did you tried to find before ask the question?. check [this](https://stackoverflow.com/q/43704113/6333971) and [How to save RecyclerView's scroll position using RecyclerView.State?](https://stackoverflow.com/q/27816217/6333971) – Bhavin Patel Feb 18 '22 at 05:32
  • Yes I tried it before but it is crashing in Fragment – Henish Shah Feb 18 '22 at 11:32

0 Answers0