Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exit animation not working; FragmentTransaction custom animation does not work for hide

I use show/hide to display a fragment that takes up part of the screen. For some reason when the fragment is shown the slide_in_left animation plays, but when the fragment is being hidden there is no animation, the fragment just disappears. I've tried using the slide_in_left animation for both exit and enter, this did not help. When a trace the code into the support package, the animation does get created and the code for displaying it is being executed. (I traced the .hide call)

FragmentManager fm = _activity.getSupportFragmentManager();
Fragment fragment = fm.findFragmentById(R.id.my_fragment);
FragmentTransaction ft = fm.beginTransaction();
ft.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_left);
if (fragment.isHidden()) {
    ft.show(fragment);
    fragment.setUserVisibleHint(true);
} else {
    ft.hide(fragment);
}
ft.commit();

Just in case here's the xml for the slide_out_left animation

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="0" android:toXDelta="-50%p"
        android:duration="@android:integer/config_mediumAnimTime"/>
    <alpha android:fromAlpha="1.0" android:toAlpha="0.0"
        android:duration="@android:integer/config_mediumAnimTime" />
</set>

Edit: It's possible that the problem has something to do with the fact that my_fragment shares screen width with another fragment cotaining a webview. When .show is executed for my_fragment it becomes visible and shares space within a horizontal linear layout (how much screen width each of the two fragments takes up is determined by the weight parameter).

like image 655
Leo K Avatar asked Dec 10 '12 21:12

Leo K


People also ask

What is Fragment transition?

The Fragment API provides two ways to use motion effects and transformations to visually connect fragments during navigation. One of these is the Animation Framework, which uses both Animation and Animator . The other is the Transition Framework, which includes shared element transitions.

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).

What is transition in Android?

Android's transition framework allows you to animate all kinds of motion in your UI by simply providing the starting layout and the ending layout.


2 Answers

Try to use transitions instead. Here is the example from my project:

private fun changeFragment(fragment: BaseFragment, addBackStack: Boolean) {
    // Set transitions
    fragment.enterTransition = Fade(Fade.IN).apply { duration = 800 }
    fragment.exitTransition = Slide(Gravity.TOP).apply { duration = 800 }
    // Your transaction below
    val fragmentTransaction = supportFragmentManager.beginTransaction()

    fragmentTransaction.replace(R.id.fragmentContainer, fragment)
    if (addBackStack) {
        fragmentTransaction.addToBackStack(fragment.javaClass.name)
    }
    fragmentTransaction.commit()
}

And also you can make your own transitions: https://medium.com/@belokon.roman/custom-transitions-in-android-f8949870bd63

like image 150
Sviatoslav Zaitsev Avatar answered Oct 21 '22 02:10

Sviatoslav Zaitsev


Try to use setCustomAnimations(int enter, int exit, int popEnter, int popExit) instead of setCustomAnimations(int enter, int exit) When you will call popupbackstack exit animation wont be called

like image 35
Amlan Avatar answered Oct 21 '22 03:10

Amlan