Nested fragments disappear during transition animation Nested fragments disappear during transition animation android android

Nested fragments disappear during transition animation


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 defaultprivate static final int DEFAULT_CHILD_ANIMATION_DURATION = 250;@Overridepublic 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).


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).


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);}@Overridepublic 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.