If you’ve architected an Android app as a single activity app switching fragments out for each screen, you’ll know that saveinstancestate doesn’t get called on the fragment. So how do you save the scroll position of a fragment you’ve just navigated away from when you navigate back to it (maybe from hitting the back button)? It’s actually very simple.
Fragments automatically save and restore the states of their Views, as long they have IDs assigned. If you ensure to assign an ID to your scrollview or nestedscrollview, its scroll state can be restored simply by doing:
<androidx.core.widget.NestedScrollView
android:id="@+id/myscrollview"
...>
Et voila!