Я думал, что система будет обращать анимацию на бэкстейдже при нажатии кнопки "назад" при использовании фрагментов с помощью следующего кода:
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();
Согласно документации android по пользовательской анимации:
Изменить:
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 );
и теперь задний стек анимируется - В обратном направлении!!!
Используйте правильную анимацию Я использовал следующее, и он работает как шарм
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>
Затем используйте следующее при добавлении фрагмента
setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_left,
R.anim.slide_out_right, R.anim.slide_in_right)
и все будет работать на 100%
в моем случае
ft.setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.slide_out_right,
R.anim.slide_in_right, R.anim.slide_out_left);
создаст прекрасную анимацию.
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>
.setCustomAnimations(R.animator.fragment_fade_in,
R.animator.fragment_fade_out,
R.animator.fragment_fade_p_in,
R.animator.fragment_fade_p_out)
Замените вышеуказанное на:
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();
эта работа для меня!! этот код для фрагмент! если вы хотите использовать этот код в активности, удалить в getActivity начало ()
!!
getActivity().getSupportFragmentManager()
.beginTransaction()
.setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.fade_out,android.R.anim.slide_in_left, android.R.anim.fade_out)
.replace(R.id.fragment_container, new YourFragment)
.addToBackStack(null)
.commit();
Удачи вам!!
Это как говорилось в классе фрагмента транзакции .
/**
* 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);
так, Наконец, вы можете использовать метод, как это
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();