How to Reverse Fragment Animations on BackStack?
42,878
Solution 1
According to the android documentation for custom animation:
Change:
ft.setCustomAnimations(R.anim.slide_in, R.anim.hyperspace_out);
To:
ft.setCustomAnimations(R.anim.slide_in, R.anim.hyperspace_out, R.anim.hyperspace_in, R.anim.slide_out );
and now the backstack animates - In reverse!!
Solution 2
Use the Correct animation I have used the following and its working like a charm
slide_in_left.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="@android:integer/config_mediumAnimTime" >
<objectAnimator
xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="500"
android:propertyName="x"
android:valueFrom="1000"
android:valueTo="0"
android:valueType="floatType" />
</set>
slide_in_right.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="@android:integer/config_mediumAnimTime" >
<objectAnimator
xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="500"
android:propertyName="x"
android:valueFrom="0"
android:valueTo="1000"
android:valueType="floatType" />
</set>
slide_out_left.xml
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="@android:integer/config_mediumAnimTime" >
<objectAnimator
xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="500"
android:propertyName="x"
android:valueFrom="0"
android:valueTo="-1000"
android:valueType="floatType" />
</set>
slide_out_right.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="@android:integer/config_mediumAnimTime" >
<objectAnimator
xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="500"
android:propertyName="x"
android:valueFrom="-1000"
android:valueTo="0"
android:valueType="floatType" />
</set>
Then Use following while adding fragment
setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_left,
R.anim.slide_out_right, R.anim.slide_in_right)
and it will worked 100%
Solution 3
in my case
fragmentTransaction.setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.slide_out_right,
R.anim.slide_in_right, R.anim.slide_out_left);
would create perfect animation.
slide_in_right
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="50%p" android:toXDelta="0"
android:duration="@android:integer/config_mediumAnimTime"/>
<alpha android:fromAlpha="0.0" android:toAlpha="1.0"
android:duration="@android:integer/config_mediumAnimTime" />
</set>
slide_out_left
<?xml version="1.0" encoding="utf-8"?>
<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>
Solution 4
.setCustomAnimations(R.animator.fragment_fade_in,
R.animator.fragment_fade_out,
R.animator.fragment_fade_p_in,
R.animator.fragment_fade_p_out)
Replace the above with:
mFragmentManager.beginTransaction()
.setCustomAnimations(R.animator.fragment_fade_in,
R.animator.fragment_fade_out,
R.animator.fragment_fade_p_in,
R.animator.fragment_fade_p_out)
.replace(R.id.main_container, FragmentPlayerInfo.getInstance(data))
.addToBackStack(FragmentPlayerInfo.TAG)
.commit();
Solution 5
This is as mentioned in Fragment Transaction class .
/**
* Set specific animation resources to run for the fragments that are
* entering and exiting in this transaction. The <code>popEnter</code>
* and <code>popExit</code> animations will be played for enter/exit
* operations specifically when popping the back stack.
*
* @param enter An animation or animator resource ID used for the enter animation on the
* view of the fragment being added or attached.
* @param exit An animation or animator resource ID used for the exit animation on the
* view of the fragment being removed or detached.
* @param popEnter An animation or animator resource ID used for the enter animation on the
* view of the fragment being readded or reattached caused by
* {@link FragmentManager#popBackStack()} or similar methods.
* @param popExit An animation or animator resource ID used for the enter animation on the
* view of the fragment being removed or detached caused by
* {@link FragmentManager#popBackStack()} or similar methods.
*/
@NonNull
public abstract FragmentTransaction setCustomAnimations(@AnimatorRes @AnimRes int enter,
@AnimatorRes @AnimRes int exit, @AnimatorRes @AnimRes int popEnter,
@AnimatorRes @AnimRes int popExit);
so finally you can use method like this
mFragmentManager.beginTransaction()
.replace(R.id.container, fragment)
.setCustomAnimations(R.anim.slide_left,//enter
R.anim.slide_out_left,//exit
R.anim.slide_right,//popEnter
R.anim.slide_out_right)//popExit
.addToBackStack(fragment.toString())
.commit();
Related videos on Youtube
![Admin](/assets/logo_square_200-5d0d61d6853298bd2a4fe063103715b4daf2819fc21225efa21dfb93e61952ea.png)
Author by
Admin
Updated on May 18, 2020Comments
-
Admin about 4 years
I thought the system would reverse animations on the backstack when the back button is pressed when using fragments using the following code:
FragmentManager fm = getFragmentManager(); FragmentTransaction ft = fm.beginTransaction(); ft.setCustomAnimations(R.anim.slide_in, R.anim.hyperspace_out); ft.replace(R.id.viewContainer, new class(), "layout").addToBackStack(null).commit();
-
AreusAstarte about 11 yearsbtw, I know this isn't connected to your question and answer, but could you maybe link me to something that explains customAnimations a little bit? :P
-
mDroidd over 10 yearsAreusAstarte: see developer.android.com/reference/android/app/…, int, int, int)
-
user3497504 almost 9 yearsHi im actually using Content transition, is working fine, but when i press back and go to previous fragment the background just disapear animateing the views but also overlying with the prevoious ones, any way to avoid this?
-
w3bshark over 8 yearsNote this will not work if you are using the support fragment manager or if your fragment extends the support version of Fragment
-
Daniels Šatcs over 8 years@w3bshark How to get such animations working using
FragmentManager
andFragment
from support library? -
w3bshark over 8 years@DanielShatz You must use translations rather than objectAnimators. For instance, slide_in_left.xml would be:
<translate android:fromXDelta="100%" android:startOffset="25" android:toXDelta="0" />
See this answer: stackoverflow.com/a/5151774/1738090 -
Wtower about 8 yearsI would recommend you to add an explanation on how your recommendation helps.
-
TarikW about 8 yearsI do not know why it work (: , but when added animation after
replace
andaddToBackstack
, is not worked -
techtinkerer almost 8 yearsI am trying this out (on Marshmallow device - didnt try other versions). It doesnt work. final FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction(); fragmentTransaction.setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_left, R.anim.enter_from_left, R.anim.exit_to_right); fragmentTransaction.replace(R.id.fl_right_container, detailFragment); fragmentTransaction.replace(R.id.fl_left_container, subcategoriesFragment, TestActivity.TAG_SUBCATEGORIES_FRAGMENT); fragmentTransaction.commit();
-
Muhammad Husnain Tahir over 7 years@TarikW i'm bit late, but order is important in this, you need to call setCostomAnimations before replace, addToBackStack methods
-
F.Mysir about 4 yearsI thought of doing it my self, but I am too lazy. And I said somebody should have post this on StackOverflow and here it is! haha
-
Hoang Nguyen Huu about 4 yearsNobody had posted this before and I quite believed that time was my turn to post this answer, to help s.o who might be in the same shoes as me... lol @F.Mysir