Hello. I'm trying to migrate my app from fragments...
# compose
s
Hello. I'm trying to migrate my app from fragments to compose. I've planned to do it fragment by fragment with a
Composeview
inside each, but I'm facing an issue with state restoration when using navigation component. When put on the backstack, the fragment's view is destroyed, but
onSaveInstanceState
is not called and there is no
saveHierarchyState
mechanism on
ComposeView
. So when a fragment's view is recreated when i pop the backstack, all scroll state of LazyColumn is lost for example. Is there a way to save this state inside fragments ? Thanks
a
could you please verify it still works like this on the latest version of fragments?
androidx.fragment:fragment:1.3.2
i
Yep, keep in mind that Navigation only depends on Fragment 1.2.X and you need Fragment 1.3.X for the proper fixes for embedding Compose in a Fragment (this particular issue should have been fixed by https://issuetracker.google.com/issues/158503763 in Fragment 1.3.0-alpha07)
Compose no longer force upgrades your version of Fragments, so make sure you have an explicit dependency on the latest version of Fragments
s
Thanks for your answers. I'm already using the version of
1.3.2
of Fragments and the latest beta of Compose. The CompositionLocal
LocalSaveableStateRegistry
is indeed using
FragmentViewLifecycleOwner
when a
DisposableSaveableStateRegistry
is created.
a
please file a bug then
s
I've put some breakpoints to see what is happening when a fragment is put on the backstack. It seems to me that all the
DisposableEffect
s created when using
rememberSaveable
are disposed, so all the entries of the registry are removed, then the
SaveableStateRegistry
is unregistered from the
FragmentViewLifecycleOwner
and finally performSave is called, but there is nothing left to be saved
ok, i'm doing it now. thanks again for your help 👍
v
Hey, did you file a bug? Can't find anything related in the tracker. I'm struggling with the same issue of losing LazyColumn scroll state on navigation.
i
s
Yes I did (https://issuetracker.google.com/issues/183851544), and as Ian said,
setViewCompositionStrategy
solved the issue
v
Thank you! I completely missed this method 😞