I'm using a bottom navigation view which has 2 menu items. Each menu item upon clicking loads a fragment onto the MainActivity
.
The first fragment is the home fragment where a RecyclerView
shows a list of items and the second fragment contains a simple form.
now whenever I switch back and forth between these fragments, their previous states are lost and the fragment is recreated. This is a big issue for me because suppose I scroll to, let's say the 15th item of the list present in the home fragment. After scrolling, I navigate to the second fragment and then I navigate back. What happens is that the home fragment gets recreated and to see the 15th item I have to scroll again.
What should I do to handle this?
Asked
Active
Viewed 40 times
0

Bashir
- 2,057
- 5
- 19
- 44

Abhishek Aditya
- 43
- 8
-
So you want the the first fragment to scroll back to the position you left the fragment at? – Lucadmin Jun 11 '20 at 15:42
-
@Lucadmin Yes. I need the recycler view to scroll back to that particular item. – Abhishek Aditya Jun 11 '20 at 18:04
-
Just safe the view on which the user clicked and then put in the onResume method a code to scroll to this position. This could help maybe: https://stackoverflow.com/questions/33328806/how-to-use-recyclerview-scrolltoposition-to-move-the-position-to-the-top-of-cu/33329765 – Lucadmin Jun 11 '20 at 18:18