Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable/avoid Fragment custom animations after screen rotation

I have just figured out that every time I setRetainInstance(true) on a Fragment it works as expected (Fragment data is retained), but this is causing the fragment's custom animation to be executed again after screen rotation.

Is there a way to avoid/disable those animations on screen rotation?

The fragment is created using the following animations:

setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left, R.anim.slide_in_left, R.anim.slide_out_right);

So, I don't want those "sliding animations" to be executed again on screen rotation.

like image 934
TatoCuervo Avatar asked Apr 11 '14 15:04

TatoCuervo


1 Answers

This is how I handled it

private boolean viewsHaveBeenDestroyed;

@Override
public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
    // This stops animation on rotation as we have a retained instance.
    boolean shouldNotAnimate = enter && viewsHaveBeenDestroyed;
    viewsHaveBeenDestroyed = false;
    return shouldNotAnimate ? AnimationUtils.loadAnimation(getActivity(), R.anim.none)
            : super.onCreateAnimation(transit, enter, nextAnim);
}

@Override
public void onDestroyView() {
    super.onDestroyView();
    viewsHaveBeenDestroyed = true;
}

Where R.anim.none is just an animation that does nothing. Good luck.

like image 77
MinceMan Avatar answered Sep 22 '22 13:09

MinceMan