Architecture-components-samples: NavigationAdvancedSample: Fragment animations run when switching tabs

Created on 29 Apr 2019  路  10Comments  路  Source: android/architecture-components-samples

When setting animations on the action action_title_to_about in home.xml navigation graph as below:
Screenshot 2019-04-29 at 17 17 29

We now get a nice fade-in fade-out animation when navigating from Home -> About.
The issue is, when we switch tabs and return to the home tab, the fade-in animation is re-run.

nav-animations

Is this intended behaviour? Or a consequence of using a separate NavHostFragment for each tab? We'd like the animations to be run when navigating within the tab, but not when switching tabs.

I'm considering this is as an issue with the samples rather than the navigation component as it may be down to how NavigationExtensions implements the attaching/detaching of the NavHostFragments.

Any help/workarounds would be appreciated 馃槃

Most helpful comment

Fixed in androidx.fragment:fragment:1.1.0-beta01
https://developer.android.com/jetpack/androidx/releases/fragment#1.1.0-beta01

All 10 comments

I found a solution. But I'm not sure.

Add the below code in Title.kt and Aboud.kt.

override fun onCreateAnimator(transit: Int, enter: Boolean, nextAnim: Int): Animator? {
    return if (parentFragment?.isResumed == true) {
        super.onCreateAnimator(transit, enter, nextAnim)
    } else {
        AnimatorSet()
    }
}

I think the behavior reason is ChildFragmentManager, not Navigation.

I also had this issue. Did someone try the workaround above?

  • If we have a lot of fragments then we need to always use it ?

@STAR-ZERO sorry for the delay, I didn't have my laptop.

I've tried the workaround and it does seem to fix the issue for me. It also worked just by using isResumed instead of parentFragment?.isResumed.
I tested with "don't keep Activities" too just in case, but the animation isn't run (which is what we want) then either.

The only downside I see is that you have to add this onCreateAnimator for every Fragment that might get shown in the BottomNavigationView or have a common base class that handles it.

My solution needs to add onCreateAnimator to every Fragment. That is not good.
I'm looking for another solution but I haven't found it yet.
Does anyone have any ideas?

Fixed in androidx.fragment:fragment:1.1.0-beta01
https://developer.android.com/jetpack/androidx/releases/fragment#1.1.0-beta01

@STAR-ZERO Any ideas when it'll come to stable ?

@iAmMONK Sorry, I have no idea.

@aidangrabe @STAR-ZERO did you guys tested the Fragment update already ?

As per the transitions behavior for bottom navigation:

Transition between active and inactive bottom navigation destinations using a cross-fade animation.

So a cross-fade is expected when switching between bottom navigation items. As mentioned above, animations running on child fragments is fixed in the Fragment 1.1.0 or higher.

Was this page helpful?
0 / 5 - 0 ratings