Wai Yan Hein Wai Yan Hein - 11 days ago 6
Android Question

Opening and closing activity with animation not working on back pressed but working for opening

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

<?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

<?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>


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?

Answer

Try to call overridePendingTransition just after your startActivity method.

Comments