问题
What approach should be taken to preserve (data & view rendering) of recyclerview list in 2 tabs present within a fragment, which is part of androidx.navigation NavController
?
Activity --> Fragment A --> Fragment B (with 2 tabs) --> Fragment C (and back till activity)
Each tab of Fragment B has accordion type view (multiple active), made from 1 ParentRecycler view & ChildRecycler view (number of parent recycler view times)
(The recycler view list are not bound to change on previous next browsing)
I tried writing onSaveInstanceState
, but that doesn't get called as Activity is never destroyed. And using bundle storage (onSaveInstanceState) will be redundant, when I write roomdb storage, for all data later. I think outState bundle will get deleted after the session?, so not much useful as compared to db.
Preserving of adapter (which recycler parents are open/closed) is a step regardless of this storage mechanism (which is the issue)
Going through other older answers I found
- For apps not built using Navigation Controller, show() - hide() was an option https://stackoverflow.com/a/14033244/1029110
- Fragment onCreateView will be called everytime navigation changes happen (popBackStack causes calling oncreateView of fragment again and again) and there view inflation is task that is bound to take place there.
What I think I can do
- Preserve expandedState in some persistent storage & allow onCreateView to remake the list every time (which can be a lot)
- Bring focus to list item, user last left/read using recyclerView.smoothScrollToPosition(position)
Instead there must be some optimized/new solution
I am not fluent in ViewModel and can't change the structure.
来源:https://stackoverflow.com/questions/60327414/preserve-view-with-data-of-recyclerview-list-in-navcontroller-fragments