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
sa...@gmail.com <sa...@gmail.com> #3
Interesting. And no reason for us to wrap it in set
either. We removed and it works as you mentioned in our main app as well.
ch...@google.com <ch...@google.com> #4
The code path resulting in the problem starts with an initial call to Animation.initializeInvalidationRegion(), which calls (in the AnimationSet override) applyTransformation() on all of the children.
applyTransformation() side-effects an instance variable called mPreviousTransform, which will be used in later calculations for translating the fragment.
Unfortunately, applyTransformation() is not overridden in AnimationSet (I believe this is an unfortunate, and very old, oversight). That means that it is stubbed out for a nested AnimationSet hierarchy.
This works for the 1-Set case because initializeInvalidationRegion() works directly in the top level AnimatorSet, so it is not stuck by the applyTransformation() problem for that first Set. But once you stick another one in the hierarchy, applyTransformation() will stub out and that initial mPreviousTransform() will be incorrect on that first frame.
Note that the blink comes from setting the fragment visible immediately. This would be fine given a correct animation transform, but given the problem above, it is incorrect for the first frame, so we see it pop into place in the wrong location for just that initial frame.
ki...@gmail.com <ki...@gmail.com> #5
I have this bug and I don't use nested <set> in animation files. It doesn't happens every time. Also it doesn't happens with FragmentManager.enableNewStateManager(false)
Sample project and video attached.
il...@google.com <il...@google.com> #6
Re #5 - you are indeed using a <set>
in your animation files, so this is the same issue:
The reason this occurs in your case is that you are wrapping your <translate> in a <set> and the new state manager wraps your Animation in its own
AnimationSet
at runtime to properly receive callbacks from the Animation, meaning there's a total of two nested sets.
So you'd see the same behavior with the old state manager if you manually wrapped your animations in another <set>
, as the example in #2 explained.
ki...@gmail.com <ki...@gmail.com> #7
I see. Thanks for explanation. I understood #2 a bit wrong then.
ap...@google.com <ap...@google.com> #8
Branch: androidx-master-dev
commit 374e0ca1083b40cceb683014c549b0921eeda68b
Author: Ian Lake <ilake@google.com>
Date: Mon Sep 14 12:40:18 2020
Avoid usages of AnimationSet for entering Animations
Adding an AnimationSet within an AnimationSet leads to
visual artifacts when used with entering Animations
due to how AnimationSet is implemented - something
that Fragments cannot control.
While we can't control developers using a <set> within
a <set> in their own Animation XML, our usage of
an additional AnimationSet meant that even a single
<set> in XML was enough to trigger the visual artifacts
and blinking.
By removing our AnimationSet, we lose the ability to
properly complete the special effect when the Animation
ends, but because that is the existing behavior of
Fragments, this is probably the best we can do while
1) maintaining the ability to use Animations
2) Avoiding visual artifacts
Test: existing tests pass, tested in sample apps
BUG: 163084315
Change-Id: Ie17af24c3920bb097cfc90fba6ca55e0cd7199ff
M fragment/fragment/src/main/java/androidx/fragment/app/DefaultSpecialEffectsController.java
M fragment/fragment/src/main/java/androidx/fragment/app/FragmentAnim.java
il...@google.com <il...@google.com> #9
Bad news: Nested AnimationSet
instances are broken at a fundamental layer that Fragments can't work around. We strongly recommend switching to Animator
or AndroidX Transition
and are doing that internally in
Good news: We're reverting to the equivalent behavior of the old state manager in that we won't use an additional layer of AnimationSet
for entering Animations. This means that unlike Animators or Transitions, a Fragment using Animation
will immediately move to RESUMED
before the Animation
completes when using the new state manager.
g....@gmail.com <g....@gmail.com> #10
The only reason why I'm using Animation
is that I need to use percent values, as in the animation of FragmentTransaction
(and by extension Navigation
) only accepts @AnimatorRes
and @AnimRes
, I can't create the animation programmatically.
Is there any alternative to Animation
in this case?
il...@google.com <il...@google.com> #11
Re #10 - note that even when you do not pass a resource ID, you can still override onCreateAnimation()
or onCreateAnimator()
on your Fragment and return any custom Animation
or Animator
you want - there is no requirement that you only use Animations/Animators inflated from XML when using Fragments (and by extension, when using Navigation).
g....@gmail.com <g....@gmail.com> #12
Thank you, that does the trick. I even used to patch animations on the fly before you fixed all the issues I had with FragmentContainerView
, how could not I think of doing that.
For those reading that might be in a similar situation:
I have few animations that I use throughout the applications and they are all defined in the actions of the navigation graph. Since I have a common base Fragment class, I added something like the following to that base class (using the animation in
@Nullable
@Override
public Animator onCreateAnimator(int transit, boolean enter, int nextAnim) {
if (nextAnim == R.anim.transition_slide_left_show) {
int width = requireActivity().getWindow().getDecorView().getWidth();
ObjectAnimator anim = ObjectAnimator.ofFloat(getView(), View.TRANSLATION_X, -width, 0);
anim.setStartDelay(300);
anim.setDuration(300);
anim.setInterpolator(new DecelerateInterpolator());
return anim;
} else {
return super.onCreateAnimator(transit, enter, nextAnim);
}
}
This is probably not ideal given that now the XML animation is ignored, that could be not very obvious (you could also not outright ignore it), but it's a quick and easy solution to turn several XML based Animation
s into Animator
s.
il...@google.com <il...@google.com> #13
For full screen slides, you should strongly consider using a Slide
Transition
yc...@gmail.com <yc...@gmail.com> #14
I have got this bug with 'sharedElementReturnTransition', it causes blinking too. Also it doesn't happens with FragmentManager.enableNewStateManager(false). Any ideas? I don't want to use it unless I have to.
il...@google.com <il...@google.com> #15
Re #14 - Transitions do not use Animations so that would be unrelated to this issue. Please file a new issue with a sample project that reproduces your issue using the latest Fragment 1.3.2.
yc...@gmail.com <yc...@gmail.com> #16
I have tried to make a sample depends in
but I can't found the blinking, maybe there is something wrong in my project, I will try more, anyway ,thanks for reply!
yc...@gmail.com <yc...@gmail.com> #17
Re #15 - I have figured out the problem! It's just because I did some Job at "onPause()" in the return blinking Fragment!
Description
Component used: Fragment
Version used: 1.3.0-Snapshot (Build Id: 6743994)
Devices/Android versions reproduced on: Samsung Galaxy S10 running Android 10
When using "setCustomAnimations" on Fragment Transitions, the animations are broken (flashing) when using the new state manager.
Steps:
FragmentManager.enableNewStateManager(false)
inMainActivity
and re-run the appAttached is a sample and 2 videos detailing the difference in animations with the state manager being turned on and off