14
votes

Here is the situation:

I am using child fragment introduced with API 17. Say I have

ActivityA -> FragmentA
ActivityA -> FragmentB

FragmentA -> ChildFragmentA
FragmentA -> ChildFragmentB

So I'm on ActivityA->FragmentA->ChildFragmentA and I transition to ActivityA->FragmentA->ChildFragmentB with a FragmentTransaction using animations for adding to the backstack and popping the backstack (there is an animation when I go to ChildFragmentB and an animation when I press back and move to ChildFragmentA).

Now I navigate to ActivityA->FragmentB (FragmentA is no long attached). When I navigate back to ActivityA->FragmentA where ChildFragmentB is visible, ChildFragmentB animates in as it did when coming from ChildFragmentA.

I want to disable this animation when resuming Activity->FragmentA. But keep it when transitioning between the children fragments. This animation is set in the FragmentTransaction. Is there any way to make this happen?

2
Have you tried keeping a value of which activity or fragment you're coming from and setting your fragmenttransaction accordingly? Like ft.setCustomAnimations(FragmentTransaction.TRANSIT_NONE, FragmentTransaction.TRANSIT_NONE); for when you don't want any transition. I'm not sure where you're specifying your FragmentTransaction so I don't know if this works for you.SvenT23

2 Answers

4
votes

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" />
0
votes

Try to disable the transition onAttach in ActivityA->FragmentA and re-enable it again onResume in the ChildFragments of both FragmentA and B