Status Update
Comments
il...@google.com <il...@google.com> #2
Branch: androidx-main
commit 57ca221882695bd6a52549f4d9ea3b812e6fe87c
Author: Simon Schiller <simonschiller@users.noreply.github.com>
Date: Mon Mar 22 16:09:30 2021
[GH] [FragmentStrictMode] Detect <fragment> tag usage
## Proposed Changes
- Detect `<fragment>` tag usage inside XML layouts
## Testing
Test: See `FragmentStrictModeTest#detectFragmentTagUsage`
## Issues Fixed
Fixes: 153738235
This is an imported pull request from
Resolves #141
Github-Pr-Head-Sha: 4ea052596e4341b9f11bcf335e2bc38045a91f19
GitOrigin-RevId: 62e7487aa4874eef6bb556490e193717cf937251
Change-Id: Iae48578e85e4e4897f806d7ade2e2a660adf9479
M fragment/fragment/api/public_plus_experimental_current.txt
M fragment/fragment/api/restricted_current.txt
M fragment/fragment/src/androidTest/java/androidx/fragment/app/strictmode/FragmentStrictModeTest.kt
M fragment/fragment/src/main/java/androidx/fragment/app/FragmentLayoutInflaterFactory.java
M fragment/fragment/src/main/java/androidx/fragment/app/strictmode/FragmentStrictMode.java
A fragment/fragment/src/main/java/androidx/fragment/app/strictmode/FragmentTagUsageViolation.java
ap...@google.com <ap...@google.com> #3
Branch: androidx-main
commit d938453e52702fb48836cbf08b751c6354a734dc
Author: Ian Lake <ilake@google.com>
Date: Tue Mar 02 10:57:20 2021
Let FragmentViewLifecycleOwner implement ViewModelStoreOwner
SavedStateViewModelFactory makes some assumptions that
the SavedStateRegistryOwner is also a ViewModelStoreOwner.
This is almost always the case, except when using the
ViewTree APIs in Fragments. This change makes it so
that the internal FragmentViewLifecycleOwner that is
tied to the Fragment's View Lifecycle implements
ViewModelStoreOwner as well (delegating to the Fragment's
ViewModelStore).
Relnote: "Using `SavedStateViewModelFactory` now works
when used with the `SavedStateRegistryOwner` returned by
using `findViewTreeSavedStateRegistryOwner()` from a
Fragment's View."
Test: updated tests pass
BUG: 181577191
Change-Id: I21acf18f70ab6dcc60f5946d5b0d878e12bc76df
M fragment/fragment/src/androidTest/java/androidx/fragment/app/FragmentViewLifecycleTest.kt
M fragment/fragment/src/main/java/androidx/fragment/app/Fragment.java
M fragment/fragment/src/main/java/androidx/fragment/app/FragmentViewLifecycleOwner.java
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.