Performing action after fragment transaction animation is finished

Performing action after fragment transaction animation is finished

fragment transition animation
android smooth fragment transition
fragment exit animation not working
android fragment transition from left to right
android flip animation between fragments
oncreateanimation fragment
fragment transaction is slow
fragment override animation

I want to set a buttons visibility after the animation is finished.

That's what calls the animation:

android.support.v4.app.FragmentTransaction fAnimation = this.getActivity().getSupportFragmentManager().beginTransaction();
fAnimation.setCustomAnimations(android.R.anim.slide_in_left, R.anim.pull_out_to_left);
if (this.isVisible()) {
    fAnimation.hide(this);
    fAnimation.commit();
}

// code that will be executed when the fragment is gone (after the animation is over)

Is there any way to attach a listener to know when my fragment is gone?


The Animators that @nmw implements in his answer were added in API Level 11 and will not work with Fragments as implemented by the Android support library.

To listen to Fragment animation events, I extended the support library's Fragment class and overrode onCreateAnimation, attaching a custom AnimationListener to the returned Animation object:

public class MyFragment extends android.support.v4.app.Fragment {

    private static final String TAG = "MyFragment";

    @Override
    public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {

        Animation anim = AnimationUtils.loadAnimation(getActivity(), nextAnim);

        anim.setAnimationListener(new AnimationListener() {

            @Override
            public void onAnimationStart(Animation animation) {
                Log.d(TAG, "Animation started.");
                // additional functionality 
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
                Log.d(TAG, "Animation repeating.");
                // additional functionality
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                Log.d(TAG, "Animation ended.");
                // additional functionality
            }
        });

        return anim;
    }
}

Smoothing performance on Fragment transitions, I want to set a buttons visibility after the animation is finished. That's what calls the animation: android.support.v4.app.FragmentTransaction  android – Performing action after fragment transaction animation is finished-ThrowExceptions. I want to set a buttons visibility after the animation is finished.


You need to subclass Fragment and override onCreateAnimator, then you can load those animations from XML and attach listeners to them.

E.g.

public class MyFragment extends Fragment
{
    @Override
    public Animator onCreateAnimator(int transit, boolean enter, int nextAnim)
    {
        final int animatorId = (enter) ? R.anim.in_anim : R.anim.out_anim;
        final Animator anim = AnimatorInflater.loadAnimator(getActivity(), animatorId);
        anim.addListener(new AnimatorListenerAdapter()
        {
            @Override
            public void onAnimationStart(Animator animation)
            {
                ...
            }

            @Override
            public void onAnimationEnd(Animator animation)
            {
               ...
            }
        });

        return anim;
   }    

Listen to FragmentTransaction animation events · GitHub, Suppose you're doing a pretty standard Fragment replacement with a custom animation: getSupportFragmentManager() .beginTransaction() . Effectuer l'action après que l'animation de la transaction de fragment est terminée when the fragment is gone (after the animation is over) Your_Action_Here


Combining the answers above here is a sample I am using successfully with the support library fragments.

Simply extend the MenuFragment and set the listener to get a callback of what to execute afterwards.

public class MenuFragment extends Fragment {

private WeakReference<OnMenuClosedListener> onMenuClosedListener;

@Override
public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
    Animation anim = null;
    if (enter) {
        anim = AnimationUtils.loadAnimation(getActivity(), R.anim.anim_slide_in_top);
    } else {
        anim = AnimationUtils.loadAnimation(getActivity(), R.anim.anim_menu_slide_out_top);
        anim.setAnimationListener(new AnimationListener() {
            @Override public void onAnimationStart(Animation animation) {
            }
            @Override public void onAnimationRepeat(Animation animation) {
            }
            @Override public void onAnimationEnd(Animation animation) {
                onMenuClosed();
            }
        });
    }

    // NOTE: the animation must be added to an animation set in order for the listener
    // to work on the exit animation
    AnimationSet animSet = new AnimationSet(true);
    animSet.addAnimation(anim);

    return animSet;
}

private void onMenuClosed() {
    if (this.onMenuClosedListener != null) {
        OnMenuClosedListener listener = this.onMenuClosedListener.get();
        if (listener != null) {
            listener.onMenuClosed();
        }
    }
}

public void setOnMenuClosedListener(OnMenuClosedListener listener) {
    this.onMenuClosedListener = new WeakReference<MenuFragment.OnMenuClosedListener>(listener);
}

/**
 * Callback for when the menu is closed.
 */
public static interface OnMenuClosedListener {

    public abstract void onMenuClosed();

}

}

FragmentTransaction, public class ObservableFragment extends Fragment {. private TransitionListener mListener;. @Override. public Animator onCreateAnimator(int  Fragment1 will be on screen when the application starts and Fragment2 will be loaded shortly after with no user action needed. This Fragment Transaction -by default- will simply replace one


Added in API 26 (and in Support Library) you can use

 FragmentTransaction runOnCommit (Runnable runnable);

For example:

FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.setCustomAnimations(R.anim.enter_from_right, R.anim.exit_from_left);
ft.show(YourFragment);
ft.commit();
ft.runOnCommit(() -> Your_Action_Here);

How to use Material Transitions in Fragment Transactions, Re-attach a fragment after it had previously been detached from the UI with detach(Fragment) . Select a standard transition animation for this transaction. as work on the main thread to be done the next time that thread is ready. to the FragmentManager's back stack, as doing so would break other  That means when you call FragmentTransaction#commit() after onSaveInstanceState() is called, the transaction won’t be remembered because it was never recorded as part of the Activity’s state in the first place. From the user’s point of view, the transaction will appear to be lost, resulting in accidental UI state loss.


Android - Fragment Transition, Action · Notification.Action. API for performing a set of Fragment operations. Like commit() but allows the commit to be executed after an activity's state is saved. Set specific animation resources to run for the fragments that are as work on the main thread to be done the next time that thread is ready. To manage fragments we need a FragmentManager that help us to handle Android fragment trasaction between fragments. With Android fragment transaction we mean a sequence of steps to add, replace or remove fragments. In the last post we showed how to support multiple screen size and orientation using fragments. Now we want to go a bit further.


Animations, starts and Fragment2 will be loaded shortly after with no user action needed. This Fragment Transaction -by default- will simply replace one Fragment with to this FragmentTransaction we can make our application gracefully animate this change like this: The following concepts are key in understanding Transitions: android.view.View.OnClickListener is an interface which has onClick (View v) method. OnClickListener is implemented to listen click event on UI component. Suppose we have a button on our UI, we can implement on click listener for it and register by calling button.setOnClickListener (listener) . Demo Project Structure in Eclipse.


Fragment Transactions & Activity State Loss, Android - Fragment Transition - Activity and Fragment transitions in Lollipop are built to launch Android emulator and verify the result of the changes done in the application. Following will be the content of res/animation/fragment_stack.​xml file. it MainActivity" android:label="@string/app_name" > <intent-filter> <​action  Multiple commits are interconnected but the fragment transaction commit does not happen immediately; A series of exceptions when memory restarting; Data saved and restored when the screen is flipped; Can not perform this action after onSaveInstanceState; Lazy loading in ViewPager and other scenarios; The animation does not perform in different


Fragment Transitions - Animates the transition as a fragment enters or exits the an app and at all scales, from finely detailed icons to key transitions and actions. uses the button's getAlpha() and setAlpha() methods to perform the animation. You can see several complete examples of activity transitions in the following  Animated fragment transition. GitHub Gist: instantly share code, notes, and snippets.