Nested fragments disappear during transition animation

fragment transition animation
android fragment transition animation library
fragment fade in animation
android fragment transaction animation
fragment exit animation not working
android shared element transition fragment
android transition animation
android flip animation between fragments

Here's the scenario: Activity contains fragment A, which in turn uses getChildFragmentManager() to add fragments A1 and A2 in its onCreate like so:

getChildFragmentManager()
  .beginTransaction()
  .replace(R.id.fragmentOneHolder, new FragmentA1())
  .replace(R.id.fragmentTwoHolder, new FragmentA2())
  .commit()

So far, so good, everything is running as expected.

We then run the following transaction in the Activity:

getSupportFragmentManager()
  .beginTransaction()
  .setCustomAnimations(anim1, anim2, anim1, anim2)
  .replace(R.id.fragmentHolder, new FragmentB())
  .addToBackStack(null)
  .commit()

During the transition, the enter animations for fragment B runs correctly but fragments A1 and A2 disappear entirely. When we revert the transaction with the Back button, they initialize properly and display normally during the popEnter animation.

In my brief testing, it got weirder - if I set the animations for the child fragments (see below), the exit animation runs intermittently when we add fragment B

getChildFragmentManager()
  .beginTransaction()
  .setCustomAnimations(enter, exit)
  .replace(R.id.fragmentOneHolder, new FragmentA1())
  .replace(R.id.fragmentTwoHolder, new FragmentA2())
  .commit()

The effect I want to achieve is simple - I want the exit (or should it be popExit?) animation on fragment A (anim2) to run, animating the whole container, including its nested children.

Is there any way to achieve that?

Edit: Please find a test case here

Edit2: Thanks to @StevenByle for pushing me to keep trying with the static animations. Apparently you can set animations on a per-op basis (not global to the whole transaction), which means the children can have an indefinite static animation set, while their parent can have a different animation and the whole thing can be committed in one transaction. See the discussion below and the updated test case project.

In order to avoid the user seeing the nested fragments disappearing when the parent fragment is removed/replaced in a transaction you could "simulate" those fragments still being present by providing an image of them, as they appeared on the screen. This image will be used as a background for the nested fragments container so even if the views of the nested fragment go away the image will simulate their presence. Also, I don't see loosing the interactivity with the nested fragment's views as a problem because I don't think you would want the user to act on them when they are just in the process of being removed(probably as a user action as well).

I've made a little example with setting up the background image(something basic).

How do you guys handle Fragments disappearing while , I've read some workaround with people swapping the nested fragment with its @Override public Animation onCreateAnimation(int transit, boolean enter,  Nested fragments disappear during transition animation Override onCreateAnimation function to your Fragment to achieve a workaround for below: Fragment clear stack with no animation behavior. A workaround for the bug where child fragments disappear when the parent is removed.

So there seem to be a lot of different workarounds for this, but based on @Jayd16's answer, I think I've found a pretty solid catch-all solution that still allows for custom transition animations on child fragments, and doesn't require doing a bitmap cache of the layout.

Have a BaseFragment class that extends Fragment, and make all of your fragments extend that class (not just child fragments).

In that BaseFragment class, add the following:

// Arbitrary value; set it to some reasonable default
private static final int DEFAULT_CHILD_ANIMATION_DURATION = 250;

@Override
public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
    final Fragment parent = getParentFragment();

    // Apply the workaround only if this is a child fragment, and the parent
    // is being removed.
    if (!enter && parent != null && parent.isRemoving()) {
        // This is a workaround for the bug where child fragments disappear when
        // the parent is removed (as all children are first removed from the parent)
        // See https://code.google.com/p/android/issues/detail?id=55228
        Animation doNothingAnim = new AlphaAnimation(1, 1);
        doNothingAnim.setDuration(getNextAnimationDuration(parent, DEFAULT_CHILD_ANIMATION_DURATION));
        return doNothingAnim;
    } else {
        return super.onCreateAnimation(transit, enter, nextAnim);
    }
}

private static long getNextAnimationDuration(Fragment fragment, long defValue) {
    try {
        // Attempt to get the resource ID of the next animation that
        // will be applied to the given fragment.
        Field nextAnimField = Fragment.class.getDeclaredField("mNextAnim");
        nextAnimField.setAccessible(true);
        int nextAnimResource = nextAnimField.getInt(fragment);
        Animation nextAnim = AnimationUtils.loadAnimation(fragment.getActivity(), nextAnimResource);

        // ...and if it can be loaded, return that animation's duration
        return (nextAnim == null) ? defValue : nextAnim.getDuration();
    } catch (NoSuchFieldException|IllegalAccessException|Resources.NotFoundException ex) {
        Log.w(TAG, "Unable to load next animation from parent.", ex);
        return defValue;
    }
}

It does, unfortunately, require reflection; however, since this workaround is for the support library, you don't run the risk of the underlying implementation changing unless you update your support library. If you're building the support library from source, you could add an accessor for the next animation resource ID to Fragment.java and remove the need for reflection.

This solution removes the need to "guess" the parent's animation duration (so that the "do nothing" animation will have the same duration as the parent's exit animation), and allows you to still do custom animations on child fragments (e.g. if you're swapping child fragments around with different animations).

Android's matryoshka problem, Animating nested fragments is half-impossible. Link: Stack Overflow Nested fragments disappear during transition animation. The bug: If you,  donc il semble y avoir beaucoup de solutions différentes pour cela, mais basé sur la réponse de @Jayd16, je pense que j'ai trouvé une solution assez solide qui permet toujours des animations de transition personnalisées sur des fragments d'enfant, et ne nécessite pas de faire une cache bitmap de la mise en page.

I was able to come up with a pretty clean solution. IMO its the least hacky, and while this is technically the "draw a bitmap" solution at least its abstracted by the fragment lib.

Make sure your child frags override a parent class with this:

private static final Animation dummyAnimation = new AlphaAnimation(1,1);
static{
    dummyAnimation.setDuration(500);
}

@Override
public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
    if(!enter && getParentFragment() != null){
        return dummyAnimation;
    }
    return super.onCreateAnimation(transit, enter, nextAnim);
}

If we have an exit animation on the child frags, they will be animated instead of blink away. We can exploit this by having an animation that simply draws the child fragments at full alpha for a duration. This way, they'll stay visible in the parent fragment as it animates, giving the desired behavior.

The only issue I can think of is keeping track of that duration. I could maybe set it to a large-ish number but I'm afraid that might have performance issues if its still drawing that animation somewhere.

Child fragments disappear when the parent is removed: Android , Nested fragments disappear during transition animation Override onCreateAnimation function to your Fragment to achieve a workaround for below: Fragment  Android Fragments and animation; android - Fragments and slide over animation; android - Replacing fragments blocks the animation of the drawer; android - Nested fragments disappear during transition animation; android - Switching between fragments while previous animation is running; android - Show and Hide Fragments without Custom Animation

Im posting my solution for clarity. The solution is quite simple. If you are trying to mimic the parent's fragment transaction animation just simply add a custom animation to the child fragment transaction with same duration. Oh and make sure you set the custom animation before add().

getChildFragmentManager().beginTransaction()
        .setCustomAnimations(R.anim.none, R.anim.none, R.anim.none, R.anim.none)
        .add(R.id.container, nestedFragment)
        .commit();

The xml for R.anim.none (My parents enter/exit animation time is 250ms)

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="0" android:toXDelta="0" android:duration="250" />
</set>

Nested fragments disappear during transition animation, Here's the scenario: Activity contains fragment A , which in turn uses getChildFragmentManager() to add fragments A1 and A2 in its onCreate like so:. The FragmentManager allows you to set transitions for any transaction. Except, the exit animations will always cause the nested fragments to disappear when the animation starts. The reason: Fragments have a “nested” lifecycle. That is, the nested fragments are stopped before their parent.

I understand this may not be able to completely solve your problem, but maybe it will suit someone else's needs, you can add enter/exit and popEnter/popExit animations to your children Fragments that do not actually move/animate the Fragments. As long as the animations have the same duration/offset as their parent Fragment animations, they will appear to move/animate with the parent's animation.

Working with Android's child fragment manager and custom transitions, With the advent of Android's fragment support library, the introduction of nested fragments, and fragment transitions, there isn't a good at the activity, the UI framework dropped the child view before the animation happened. How to use Material Transitions in Fragment Transactions will follow the Enter Transition and elements that disappear will use the Exit Transition. the app you will see some animations on

Android nested fragments, Nested fragments are only supported when added to a fragment dynamically. In Android all Nested fragments disappear during transition animation Override  What happens is that that the fragment is launched successfully and with transition, but there are two issues. When clicking the back button no transition happens at all, and the fragment just disappears with no animation. When I click the button that launches the fragment again, the app crashes with that message.

Creating and Using Fragments · codepath/android_guides Wiki , During the transition, the enter animations for fragment B runs correctly but fragments A1 and A2 disappear entirely. When we revert the transaction with the​  I have a recyclerview items inside a fragment when I click on an item from the recyclerview it repleces the olde fragment with a new one. And i have an animation going when the transition happens l

FragmentTransaction, A Fragment is a combination of an XML layout file and a java class much like an Activity . fragments presented from within another fragment or nested child fragments. Activities can initialize fragments with data during construction; Activities can Settings with PreferenceFragment · Shared Element Activity Transition  But when i'am trying to achieve similar effect within fragment which is nested in target activity this approach doesn't work. Funny thing is that enter animation is not showed, but exit animation is working fine . Another even more complicated view hierarchy is

Comments
  • What is R.id.fragmentHolder with respect to A, A1, A2, etc?
  • fragmentHolder is an id in the activity's layout, fragment{One,Two}Holder are in fragment A's layout. All three are distinct. Fragment A was initially added in fragmentHolder (i. e., fragment B is replacing fragment A).
  • I have created a sample project here: github.com/BurntBrunch/NestedFragmentsAnimationsTest , there's also an apk included in the repository. This is a really annoying bug and I'm looking for a way to work around it (assuming that it's not in my code).
  • I know a bit more about this issue now. The reason the fragments disappear is because the children handle the lifecycle events before the parent. In essence, A1 and A2 are removed before A and since they don't have animations set, they disappear abruptly. A way to somewhat mitigate this is to explicitly remove A1 and A2 in the transaction that replaces A. That way, they animate when they exit, however their animation speed is squared, since the parent container is also animating. A solution which doesn't produce this artefact would be appreciated.
  • The change(replacing the starter fragment) you mention in the question is the real one you want to do or it's just an example? You'll call the changeFragment method only one time?
  • I've decided to award you the bounty, since that was the solution I ended up using. Thank you so much for your time!
  • Wow, so filthy. The lengths us Android developers have to go to just for some slickness
  • i Have a Viewpager From Second tab i'm replacing other fragment and when i'm pressing back on it i need to show viewpager second tab,it is opening but it is showing blank page. I tried what you suggested in the above thread but still it is same.
  • Problem remains when user comes back as @Harish wrote
  • Wow seriously? its 2018 and this is still a thing? :(