Preserve view with data of recyclerview list in NavController Fragments

旧巷老猫 提交于 2020-04-18 05:30:48

问题


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

  1. For apps not built using Navigation Controller, show() - hide() was an option https://stackoverflow.com/a/14033244/1029110
  2. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!