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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With