How to animate fragment removal
I saw this when I was having similar problems and just thought Id drop a quick note.
Rather than creating a dummy fragment in order to replace the existing one I think you should animate the current fragments view. When the animation finishes you can simply remove the fragment.
This is how i did it:
final FragmentActivity a = getSherlockActivity();
if (a != null) {
//Your animation
Animation animation = AnimationUtils.loadAnimation(a, R.anim.bottom_out);
animation.setDuration(getResources().getInteger(android.R.integer.config_shortAnimTime));
//You can use AnimationListener, MagicAnimationListener is simply a class extending it.
animation.setAnimationListener(new MagicAnimationListener() {
@Override
public void onAnimationEnd(Animation animation) {
//This is the key, when the animation is finished, remove the fragment.
try {
FragmentTransaction ft = a.getSupportFragmentManager().beginTransaction();
ft.remove(RestTimerFragment.this);
ft.commitAllowingStateLoss();
} catch (Exception e) {
e.printStackTrace();
}
}
});
//Start the animation.
getView().startAnimation(animation);
}
I figured it out.
The exiting view is animated on the canvas of the entering view so if there is no entering canvas there is no canvas for the animation.
To show the animation I had to always use replace and use entering fragments of the same size to those exiting. After the animation is finished I set the view of the new fragments to gone.