0

I am currently trying to implement a custom animation when changing fragments. The custom animation only works one way and does not work when back is pressed. On back press the correct fragment is shown but with no animation. My code for the fragment transaction:

FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_right, R.anim.slide_in_right, R.anim.slide_out_left);
transaction.replace(R.id.frame_container, fragment, FragmentTag);
transaction.addToBackStack(null);
transaction.commit();

My code for the anim XML files:

//Slide in left
<?xml version="1.0" encoding="utf-8"?>
<set>
    <objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
        android:propertyName="x"
        android:valueType="floatType"
        android:valueFrom="-1280"
        android:valueTo="0"
        android:duration="500"/>
</set>

//Slide out right
<?xml version="1.0" encoding="utf-8"?>
<set>
    <objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
        android:propertyName="x"
        android:valueType="floatType"
        android:valueFrom="0"
        android:valueTo="-1280"
        android:duration="500"/>
</set>

//Slide in Right
<?xml version="1.0" encoding="utf-8"?>
<set>
    <objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
        android:propertyName="x"
        android:valueType="floatType"
        android:valueFrom="-1280"
        android:valueTo="0"
        android:duration="500"/>
</set>

//Slide out Left
<?xml version="1.0" encoding="utf-8"?>
<set>
    <objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
        android:propertyName="x"
        android:valueType="floatType"
        android:valueFrom="0"
        android:valueTo="-1280"
        android:duration="500"/>
</set>

The code for onBackPressed()

@Override
public void onBackPressed()
{
    super.onBackPressed();
}

Any ideas where I'm going wrong? Thanks for any help

Calum
  • 104
  • 11

1 Answers1

1

Use this code for onBackPressed:

if (getFragmentManager().getBackStackEntryCount() > 0) {
            getFragmentManager().popBackStack();
        } else {
            super.onBackPressed();
        }
Udayaditya Barua
  • 1,151
  • 12
  • 26