Search code examples
androidanimationthemesandroid-4.0-ice-cream-sandwich

Why my close activity animation doesn't work on Android 4.0 (ICS)


I made a theme with a custom animation (slide up and slide down). The animation works fine on the older android versions.. However, when I try it out on Android 4.0 (ICS) the on close animation doesn't work. Only the slide up animation works fine on ICS.

Here is my theme I use for the animation:

<style name="myTheme" parent="android:Theme.Black">
    <item name="android:windowTitleSize">45dip</item>
    <item name="android:windowTitleBackgroundStyle">@style/CustomWindowTitleBackground</item>
    <item name="android:windowAnimationStyle">@style/myTheme.Window</item>
</style>

<style name="myTheme.Window" parent="@android:style/Animation.Activity">
    <item name="android:activityOpenEnterAnimation">@anim/push_up_in_no_alpha</item>
    <item name="android:activityOpenExitAnimation">@anim/no_anim</item>
    <item name="android:activityCloseEnterAnimation">@anim/no_anim</item>
    <item name="android:activityCloseExitAnimation">@anim/push_down_out_no_alpha</item>
</style>

And here is push_down_out_no_alpha.xml:

<set xmlns:android="http://schemas.android.com/apk/res/android">
        <translate android:fromYDelta="0" android:toYDelta="100%p"
        android:duration="@android:integer/config_longAnimTime"/>
</set>

When i set a animation in code it also works fine on ICS, but why not as a theme?

 this.overridePendingTransition(R.anim.no_anim,R.anim.push_down_out_no_alpha);

Does anyone know why it isn't working on Android 4.0 (ICS)?


Solution

  • Specifying animations from the manifest appears to be broken in ICS :-( The override animation solution works fine, but you probably don't want to hard-code the animations. It would be nice to get them from the manifest as you would for other versions of the platform.. so....

    add a couple of member fields to your activity to hold the ids of the animations attached to your activity..

    protected int activityCloseEnterAnimation;
    protected int activityCloseExitAnimation;
    

    and somewhere in your onCreate...

    // Retrieve the animations set in the theme applied to this activity in the
    // manifest..
    TypedArray activityStyle = getTheme().obtainStyledAttributes(new int[] {android.R.attr.windowAnimationStyle});
    int windowAnimationStyleResId = activityStyle.getResourceId(0, 0);      
    activityStyle.recycle();
    
    // Now retrieve the resource ids of the actual animations used in the animation style pointed to by 
    // the window animation resource id.
    activityStyle = getTheme().obtainStyledAttributes(windowAnimationStyleResId, new int[] {android.R.attr.activityCloseEnterAnimation, android.R.attr.activityCloseExitAnimation});
    activityCloseEnterAnimation = activityStyle.getResourceId(0, 0);
    activityCloseExitAnimation = activityStyle.getResourceId(1, 0);
    activityStyle.recycle();
    

    then wherever your activity finishes/should apply animation include...

    overridePendingTransition(activityCloseEnterAnimation, activityCloseExitAnimation);
    

    and your activities should correctly honour the animations you set in the theme/style attached to activities in your manifest.