I have a Fragment A (say) which contains a FragmentPagerAdaptor, which further contains Fragments (having list view).
When I click on a list item, I replace the complete Fragment A with another fragment (say B), I have also added the transaction for Fragment A in the back stack; now when I press the back button from B, the list in the child Fragment of A gets recreated and scrolled to top.
I want to retain the state(scroll) of the list, I tried storing the scrollY of the list and setting it again, but it's inaccurate and the list takes some time to initialize, also it takes time to scroll (since the list can have thousands of items).
I came across this link to resolve the issue:
but setting the setRetainInstance(true) in child Fragments crashes the app saying:
java.lang.IllegalStateException: Can't retain fragements that are nested in other fragments
It is also a known bug in google forums:
Any kind of help will be greatly appreciated!
I want to retain the state(scroll) of the list, i have tried storing the scrollY of the list and setting it again, but it's inaccurate and the list takes some time to initialize, also it takes time to scroll(since the list can have thousands of items).
this is the recomended aproach, retained fragments were designed to maintain data not UI elements, also nested fragments have some limitations. There are SO's on how to maintain list scroll position, ie.: Maintain/Save/Restore scroll position when returning to a ListView. So you should be able to find a working solution.
If you have some heavy data structure for your list, you can put it inside non nested retained fragment with no UI. This fragment will not be destroyed during config changes (like screen rotation), but it will still be destroyed when you close your app and Android decides to kill your process. So This may cause crashes if you forgot about this case, you must prepare your app for it.
Other option would be to store you list data in sqlite database, it would be immune to config changes, but probably updates of sqlite might be slow.