I solved this by adding some logic to the parent fragment to detect when it is being hidden or shown, and explicitly disable or enable animations in its child.
@Override
public void onPause() {
super.onPause();
// If this fragment is being closed/replaced then disable animations
// in child fragments. Otherwise we get very nasty visual effects
// with the parent and child animations running simultaneously
ChildFragment f = (ChildFragment) getChildFragmentManager()
.findFragmentByTag(FRAGMENT_CHILD);
if (f != null) {
f.disableAnimations();
}
}
@Override
public void onResume() {
super.onResume();
// if this fragment is being opened then re-enable animations
// in child fragments
ChildFragment f = (ChildFragment) getChildFragmentManager()
.findFragmentByTag(FRAGMENT_CHILD);
if (f != null) {
f.enableAnimations();
}
}
In the child fragment, we need to implement those methods to enable/disable animations. We do this by overriding onCreateAnimation()
and using a static animation (R.anim.hold
) in the case where animations should be disabled.
private boolean mDisableAnimations;
void disableAnimations() {
mDisableAnimations = true;
}
void enableAnimations() {
mDisableAnimations = false;
}
@Override
public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
if (mDisableAnimations) {
return AnimationUtils.loadAnimation(getActivity(), R.anim.hold);
}
return super.onCreateAnimation(transit, enter, nextAnim);
}
The static animation is defined in res/anim/hold.xml
as:
<?xml version="1.0" encoding="utf-8"?>
<translate
xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="0"
android:toXDelta="0"
android:duration="2000" />