Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fragment hide animation is not playing

I'm trying to toggle my fragment with slide animation

Using this code:

FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();        
SearchPanelFragment existingFragment = (SearchPanelFragment) manager.findFragmentByTag(SearchPanelFragment.FRAGMENT_NAME);        
transaction.setCustomAnimations(R.xml.slide_down_search_panel, R.xml.slide_up_search_panel);        
if (existingFragment != null) {
    if (existingFragment.isVisible())
        transaction.remove(existingFragment);       
} else {
    transaction.add(R.id.top_panel_fragment, new SearchPanelFragment(this), SearchPanelFragment.FRAGMENT_NAME);
}

    transaction.commit();

So far I get only enter animation on transaction.add When is exit animation played? I could only get it when using transaction.replace but then I was just swaping an old fragment with identical new one, and what I want is to hide/remove/detach/whatever it's neccessary to make it dissapear with exit animation played

EDIT: I tried hide, remove and detach. No matter what I do, the animation is not played. It's played only on add, show, and replace

EDIT 2: There might be something wrong with the second animation. Please take a look on both on them. First one slides down and seems to be working just fine.

Slide down

<translate
    android:duration="500"
    android:fromXDelta="0%"
    android:fromYDelta="-100%"
    android:toXDelta="0%"
    android:toYDelta="0%" />

Slide up

<translate
    android:duration="700"
    android:fromXDelta="0%"
    android:fromYDelta="0%"
    android:toXDelta="0%"
    android:toYDelta="-100%" />
like image 895
Jacek Kwiecień Avatar asked Dec 18 '12 13:12

Jacek Kwiecień


2 Answers

You're actually using the wrong function.

According to the documentation for setCustomAnimations(int enter, int exit):

Set specific animation resources to run for the fragments that are entering and exiting in this transaction. These animations will not be played when popping the back stack.

Instead you should use setCustomAnimations (int enter, int exit, int popEnter, int popExit):

Set specific animation resources to run for the fragments that are entering and exiting in this transaction. The popEnter and popExit animations will be played for enter/exit operations specifically when popping the back stack.

like image 141
vinc3m1 Avatar answered Nov 18 '22 19:11

vinc3m1


Edit

The bug has been fixed as of June 7th 2013. I believe starting at revision 18, the bug has been fixed.

This no longer should be an issue if you are using the latest Support Library Revision

Old Answer

http://code.google.com/p/android/issues/detail?id=32405 There is a bug in support library. The only chance to get it work is get support library sources and recompile it yourself

like image 30
Artem Zelinskiy Avatar answered Nov 18 '22 21:11

Artem Zelinskiy