How to disable / exclude custom fragment animations after screen rotation

I just realized that every time I set RetainInstance (true) on a fragment, it works as expected (fragment data is saved), but this causes the user animation of the fragment to repeat after rotating the screen.

Is there any way to avoid / disable these animations while rotating the screen?

A 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 do not want these “sliding animations” to run on the screen again.

+5
android rotation fragment
source share
1 answer

Here's how I dealt with 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.

+10
source share

All Articles