In my application I’m doing the following (using the support library):
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.setCustomAnimations(R.anim.fade_in, R.anim.fade_out);
transaction.replace(R.id.fragment_container, fragment);
transaction.commit();
The animations R.anim.fade_in and R.anim.fade_out have less than 200 milliseconds duration.
The first time i change the fragment, the animation doesn’t show at all. I think it is due to XML inflating: the time it takes to inflate XML is bigger than the duration of the animation. From the second time on, the animation displays correctly.
Is there a way to preload an XML animation?
I found it myself. You have to override the
onCreateAnimation()method in yourFragment. This is how I did it:Note that the
mEnterAnimationandmExitAnimationfields are declared static. To load an animation viaAnimationUtils.loadAnimation()you need aContext. TheContextis obtained via theMyApplicationsingleton. To create such a singleton just write into yourAndroidManifest.xml:Then create the class
com.example.app.MyApplicationas a singleton: