Activity exit animations don't work as expecte

2020-05-20 07:35发布

问题:

I have a theme that changes the activity's open/close/enter/exit animations:

<style name="down_up_theme" parent="Theme.rtlfr">
    <item name="android:windowAnimationStyle">@style/down_up_animation</item>
</style>

<style name="down_up_animation" parent="@android:style/Animation.Activity">
    <item name="android:activityOpenEnterAnimation">@anim/slide_in_top</item>
    <item name="android:activityOpenExitAnimation">@anim/hold</item>
    <item name="android:activityCloseEnterAnimation">@anim/hold</item>
    <item name="android:activityCloseExitAnimation">@anim/slide_out_bottom</item>
</style>

And in the manifest:

<activity android:name=".activity.ArticlesActivity"
        android:theme="@style/down_up_theme" />

The goal is to make the activity content slide down on start, and slide up at exit.

The animations work fine on 2.3. On 4.0, though, the exit animation (slide up) doesn't work. What it does animate is the closing of the activities that are spawned from this activity. In my case, I want to animate the closing of the activity with the list of articles, instead the closing of the article detail has the slide up animation.

I guess I could try to add the closing animation to the activity that spawns the one I want to animate, but it actually spawns activities that should have different animations. I also couldn't find any information on this 2.3 vs. 4.0 difference in the documentation.

How can I make my animations work on 4.0?

回答1:

I'm not sure why the exit animation set in the theme is not working on ICS+, but calling overridePendingTransition() seems to be working. The simplest way to do this for you is probably to override finish() in your Activity:

@Override
public void finish() {
    super.finish();
    overridePendingTransition(R.anim.hold, R.anim.slide_out_bottom);
}


回答2:

I'd like to add just a little extra to this answer; 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.



回答3:

I was confused by this problem, too. But fortunately soon later I found what the answer was. You should check your animation file whether its root element is "Set" or not. If it's not, you should wrap it with "Set" element and then ActivityCloseAnimation attribute would work fine. I have tried it. Hope it could help you.



回答4:

you shoud look here use overridePendingTransition and windowEnterAnimation/windowExitAnimation shoud work for you