Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where is the documentation for Fragment.onCreateAnimator()? [closed]

Tags:

The entire documentation for the Fragment.onCreateAnimator(int, boolean, int) method consists of the following text:

"Called when a fragment loads an animation."

That's it. No explanation about the parameters.

What do the parameters mean? Even the source code doesn't reveal much.

like image 567
Nathan Osman Avatar asked Jun 04 '13 05:06

Nathan Osman


People also ask

How do you know if a fragment is destroyed?

Since all fragments are destroyed if the activity is destroyed, a simple answer could be calling getActivity(). isDestroyed() returning true if the activity is destroyed, therefore the fragment is destroyed.


1 Answers

The onCreateAnimator method is odd. The prototype I've seen is this:

public Animator onCreateAnimator(int transit, boolean enter, int nextAnim)

int transit - transition type, as sandrstar said above

boolean enter - true if 'entering,' false otherwise

int nextAnim - The resource ID of the animation that is about to play.

So, for example, if you tried doing a card flip, from the documentation:

// Create and commit a new fragment transaction that adds the fragment for the back of
// the card, uses custom animations, and is part of the fragment manager's back stack.
BackOfCardFragment backFragment = new BackOfCardFragment();

getFragmentManager()
    .beginTransaction()

    // Replace the default fragment animations with animator resources representing
    // rotations when switching to the back of the card, as well as animator
    // resources representing rotations when flipping back to the front (e.g. when
    // the system Back button is pressed).
    .setCustomAnimations(
         R.animator.card_flip_right_in, R.animator.card_flip_right_out,
         R.animator.card_flip_left_in, R.animator.card_flip_left_out)

    // Replace any fragments currently in the container view with a fragment
    // representing the next page (indicated by the just-incremented currentPage
    // variable).
    .replace(R.id.container_view, backFragment)

    // Add this transaction to the back stack, allowing users to press Back
    // to get to the front of the card.
    .addToBackStack(null)

    // Commit the transaction.
    .commit();

NOTE: R.id.container_view in the above example is the ID of a ViewGroup that contains the existing fragment you are trying to replace.

When the above code is executed, the onCreateAnimator method will get called, and the nextAnim parameter will be one of the four animation IDs passed into the setCustomAnimations() function, i.e. R.animator.card_flip_right_in, R.animator.card_flip_right_out... etc.

This doesn't seem immediately useful at first, since it doesn't give you a reference to the actual Animator object that you could attach a listener to. But oddly, you can just inflate another Animator directly from the nextAnim resource, and then attach listeners to that, which will, oddly, fire all the overridden callbacks at the right times:

@Override
public Animator onCreateAnimator(int transit, boolean enter, int nextAnim) {
    Animator animator = null;
    // In this example, i want to add a listener when the card_flip_right_in animation
    // is about to happen.
    if (nextAnim == R.animator.card_flip_right_in) {
        animator = AnimatorInflater.loadAnimator(getActivity(), nextAnim);
        // * Sometimes onCreateAnimator will be called and nextAnim will be 0, 
        //   causing animator to be null.
        // * I wanted to add a listener when the fragment was entering - 
        //   your use case may be different.
        if (animator != null && enter) {

            animator.addListener(new Animator.AnimatorListener() {
                @Override
                public void onAnimationStart(Animator animation) {
                   // Do something when the card flip animation begins
                }

                @Override
                public void onAnimationEnd(Animator animation) {
                   // Do something as soon as the card flip animation is over
                }

                @Override
                public void onAnimationCancel(Animator animation) {
                }

                @Override
                public void onAnimationRepeat(Animator animation) {
                }
            });
        }
    }
    return animator;
}

In this way, you should be able to add listeners to the fragment transition animators as if you had created them yourself.

like image 59
lim Avatar answered Sep 19 '22 10:09

lim