Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Custom Animation on fragment transaction not running

I'm using Google API 8 (Android 2.2) with support package v4.

It doesn't give any error or animation.

Transaction:

FragmentTransaction transaction = manager.beginTransaction();        transaction.replace(R.id.content, myFragment); transaction.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_right); transaction.commit(); 

Animations:

slide_in_left.xml

<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" >     <translate         android:duration="700"         android:fromXDelta="-100%"         android:toXDelta="0%" >     </translate> </set> 

slide_out_right.xml

<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android">     <translate         android:duration="700"         android:fromXDelta="0%"         android:toXDelta="100%" >     </translate> </set> 

Does anyone know what is happening here?

like image 657
adheus Avatar asked Jun 14 '12 21:06

adheus


People also ask

How to animate fragment transition in android?

At a high level, here's how to make a fragment transition with shared elements: Assign a unique transition name to each shared element view. Add shared element views and transition names to the FragmentTransaction . Set a shared element transition animation.

How to use fragment transaction in android?

Use replace() to replace an existing fragment in a container with an instance of a new fragment class that you provide. Calling replace() is equivalent to calling remove() with a fragment in a container and adding a new fragment to that same container. transaction.

Which kind of fragment transaction is used for removing a fragment from the screen?

The FragmentManager class and the FragmentTransaction class allow you to add, remove and replace fragments in the layout of your activity at runtime.

How to remove fragment from FragmentManager?

You need add/commit fragment using one tag ex. "TAG_FRAGMENT". Fragment fragment = getSupportFragmentManager(). findFragmentByTag(TAG_FRAGMENT); if(fragment !=


1 Answers

The manager was stacking my transaction before I set the animation, so it stacks the transaction without animations (sad but true), and that occurs even if I commit the transaction after the setCustomAnimations().

The solution is to set the animations first:

FragmentTransaction transaction = manager.beginTransaction();        transaction.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_right); transaction.replace(R.id.content, myFragment); transaction.commit(); 
like image 184
adheus Avatar answered Oct 11 '22 01:10

adheus