动画的打开和关闭活动对背面不起作用,但对打开起作用 [英] Opening and closing activity with animation not working on back pressed but working for opening
问题描述
我正在开发一个Android应用程序.在我的应用中,我正在打开一个带有动画的活动(从左向右滑动).我还希望它紧靠右后向左滑动.但是动画只对活动起作用.无法关闭.它只是正常关闭而没有幻灯片动画.请在下面查看我的代码.
I am developing an Android app. In my app, I am opening an activity with animation (Sliding left to right). I also want it close sliding right to left on back pressed. But Animation only working on activity open. Not working on close. It just close normally without slide animation. Please see my code below.
slide_in.xml
slide_in.xml
<?xml version="1.0" encoding="utf-8"?>
<translate
xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="@android:integer/config_longAnimTime"
android:fromXDelta="100%p"
android:toXDelta="0%p">
</translate>
slide_out.xml
slide_out.xml
<?xml version="1.0" encoding="utf-8"?>
<translate
xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="@android:integer/config_longAnimTime"
android:fromXDelta="0%p"
android:toXDelta="-100%p">
</translate>
在第二个活动的onCreate内
Inside onCreate of second activity
overridePendingTransition(R.anim.slide_in, R.anim.slide_out);
但是它仅适用于开幕活动.动画无法用于结束活动.我的代码有什么遗漏或出了什么问题?如何在结束活动时使用它?
But it is only working for opening activity. Animation not working for closing activity. What is missing or wrong with my code? How can I get it work on closing activity?
推荐答案
尝试在startActivity
方法之后立即调用overridePendingTransition
.
Try to call overridePendingTransition
just after your startActivity
method.
这篇关于动画的打开和关闭活动对背面不起作用,但对打开起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!