Status Update
Comments
il...@google.com <il...@google.com> #2
ap...@google.com <ap...@google.com> #3
Great! Thanks a lot, I'll look for the live updates soon!
il...@google.com <il...@google.com> #4
This has been fixed internally and we're looking to include it in the Fragment 1.3.1 release.
tf...@gmail.com <tf...@gmail.com> #5
Are there any plans to include this in any of the next releases? I just checked 1.3.2 and it doesn't seem to be there.
il...@google.com <il...@google.com> #6
Sorry, looks like this slipped through the cracks. I'll actually add this to the next release's hotlist.
b9...@gmail.com <b9...@gmail.com> #7
I upgrade 1.3.3 from 1.3.2 and can't create ViewModel in compose via androidx.lifecycle.viewmodel.compose.viewModel<T>()
anymore.
il...@google.com <il...@google.com> #8
Re #7 - please file a new bug with a sample project that reproduces your issue.
b9...@gmail.com <b9...@gmail.com> #9
Re #8 I could point out the code directly. I don't know how to describe the issue.
public ViewModelProvider(@NonNull ViewModelStoreOwner owner) {
this(owner.getViewModelStore(), owner instanceof HasDefaultViewModelProviderFactory
? ((HasDefaultViewModelProviderFactory) owner).getDefaultViewModelProviderFactory()
: NewInstanceFactory.getInstance());
}
FragmentViewLifecycleOwner
didn't implement HasDefaultViewModelProviderFactory
, so that NewInstanceFactory
will be used to create ViewModel. Which means DI framework like Hilt will failed.
il...@google.com <il...@google.com> #10
Re #9 - please file a new bug so we can track that work.
b9...@gmail.com <b9...@gmail.com> #11
Re #10,
Description
Component used:
lifecycle-viewmodel-ktx
Version used: 2.3.0
Devices/Android versions reproduced on: Pixel 4 running Android 10
Sample project to trigger the issue:https://github.com/tfcporciuncula/view-tree-owner-bug
You can repro the issue by cloning that repo, running it, and then rotating the screen causing a config change.
This is the code I'm using to create a here ):
ViewModel
in aView
(as you can seeEverything works fine, but the app crashes on orientation change with the following stacktrace:
The crash has a pretty clear cause which is this :
So it seems there's this implicit requirement of having the , which is only a
SavedStateRegistryOwner
also be aViewModelStoreOwner
, and that's not the case for the view when it's in a fragment: itsSavedStateRegistryOwner
is aFragmentViewLifecycleOwner
SavedStateRegistryOwner
and not aViewModelStoreOwner
.It feels like
findViewTreeSavedStateRegistryOwner()
should return the fragment itself (likefindViewTreeSavedStateRegistryOwner()
) instead of theFragmentViewLifecycleOwner
, so this would work. Or am I doing something wrong here?If the view is in an activity everything works fine --
findViewTreeSavedStateRegistryOwner()
returns the activity in this case, which is also aViewModelStoreOwner
so everything is good.