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?
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.
}
Added in API 26 (and in Support Library) you can use
For example:
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 overrodeonCreateAnimation
, attaching a custom AnimationListener to the returned Animation object:I had to do this in Xamarin. My situation was I needed a callback once the fragment animation ended. Here is how I made it work without any flickering (this is C#/Xamarin):
Note:
Animation.IAnimationListener
AnimationSet
otherwise theFragmentManager
will override your listener and the callbacks won't fire.You need to subclass Fragment and override onCreateAnimator, then you can load those animations from XML and attach listeners to them.
E.g.