Java – overridePendingTransition only shows entry animations

overridePendingTransition only shows entry animations… here is a solution to the problem.

overridePendingTransition only shows entry animations

When the user changes the locale, I want to reload the activity with the new locale. I want to create an animated transition when I finish the activity and start it again.

The transition animation is as follows:

The exit animation is to zoom the activity to the center of the screen.
The entry animation is to scale the activity from the center of the screen.

finish();
overridePendingTransition(0, R.anim.scale_to_center);
Intent intent =new Intent(SettingsActivity.this, SettingsActivity.class);
startActivity(intent);
overridePendingTransition(R.anim.scale_from_center, 0);

My scale_to_center.xml is:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <scale android:fromYScale="1.0" android:toYScale="0"
        android:fromXScale="1.0" android:toXScale="0" 
        android:pivotX="50%" android:pivotY="50%"
        android:duration="500"/>
</set>

My scale_from_center.xml is:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <scale android:fromYScale="0" android:toYScale="1.0"
        android:fromXScale="0" android:toXScale="1.0" 
        android:pivotX="50%" android:pivotY="50%"
        android:startOffset="500"
        android:duration="2000"/>
</set>

The problem is that only the entry conversion appears and the exit conversion does not. I tried to add a delay for the exit transition, but it didn’t work either.

However, when I change the code to only animate the exit of the application. It worked.

finish();
overridePendingTransition(0, R.anim.scale_to_center);

Thank you.

Solution

Set two animations on the overridePendingTransition method and call finish: after calling startActivity

Intent intent = new Intent(SettingsActivity.this, SettingsActivity.class);
startActivity(intent);
overridePendingTransition(R.anim.scale_from_center, R.anim.scale_to_center);
finish();

Related Problems and Solutions