Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fragment standard transition not animating

I'm using the v4 android compatibility library to develop a tablet UI using fragments specifically for Android 2.2 devices and up.

Everything is working as it should, except that I can't get any animations to work, not even the standard animations.

Code:

FragmentTransaction ft = getSupportFragmentManager().beginTransaction();     ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);     ABCFragment abcFragment = new ABCFragment();     ft.replace(R.id.main_frame_layout_fragment_holder,abcFragment);          ft.addToBackStack(null);     ft.commit(); 

Instead of using a transit animation, the fragment freezes for about a second and the just disappears and the new one appears.

Using:

ft.setCustomAnimations(android.R.anim.slide_in_left,android.R.anim.slide_out_right); 

doesn't work either.

XML:

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res/com.synergygb.mycustomapp" android:id="@+id/LinearLayout01" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:gravity="bottom"> <FrameLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/main_frame_layout_fragment_holder"> </FrameLayout> <!-- OTHER FIXED UI ELEMENTS--> </RelativeLayout> 

I read that the custom animation were broken in the compatibility library, but no one seems to be having issues with the standard transitions. I've tested this on a 3.2.1 Motorola Xoom, 2.3 Galaxy Tab 7", 2.2 emulator, and even on a HTC G2 with 2.3.4.

What could be wrong here?

like image 728
blindstuff Avatar asked Oct 10 '11 20:10

blindstuff


People also ask

How do you animate fragment transitions?

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 do you animate a fragment?

To animate the transition between fragments, or to animate the process of showing or hiding a fragment you use the Fragment Manager to create a Fragment Transaction . Within each Fragment Transaction you can specify in and out animations that will be used for show and hide respectively (or both when replace is used).


2 Answers

I finally got this to work after much trial and error.

First and foremost, get the lastest ACL, it did fix custom animations, and while this was not my exact problem, once those worked I ended up using them instead of standard transitions.

Right now I am using:

ft.setCustomAnimations(android.R.anim.fade_in,android.R.anim.fade_out,android.R.anim.fade_in,android.R.anim.fade_out); 

The key to making it work on both Android 2.1, 2.2 and 2.3, as well as Android 3.0+ was to do the following:

  • Make sure you are using ONLY API´s available to the lowest API LEVEL you wish to support (in my case 2.1).
  • Compile using Android 3.0.
  • In the manifest file, set android:hardwareAccelerated="true" inside your application tag.

Fragment animations now work on all devices. If you do not set the extra info in the application tag, the animation will occur, but in a very very choppy way, making it seem as though it did not happen at all.

Hope this helps someone in the future!

As a note, there are some API checking tools so you are sure you are not using any APIs that aren't available to you. I prefer to work on 2.1 so the IDE doesn't show anything I can't use, once I have stable code I jump back to compiling on 3.0

like image 52
blindstuff Avatar answered Sep 29 '22 20:09

blindstuff


Try getting the most recent ACL again, they have fixed it: http://code.google.com/p/android/issues/detail?id=15623#c19

Also I noticed that for setCustomAnimations, it needs to be set before transaction calls like replace in order to take effect.

FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); ft.setCustomAnimations(R.anim.in_from_left, R.anim.out_to_right, R.anim.in_from_right, R.anim.out_to_left); ft.replace(android.R.id.content, newFrag); ft.addToBackStack(null); ft.commit(); 
like image 41
dvd Avatar answered Sep 29 '22 20:09

dvd