I wanted to use the styles.xml solution, but it did not work for me with activities.
Turns out that instead of using android:windowEnterAnimation
and android:windowExitAnimation
, I need to use the activity animations like this:
<style name="ActivityAnimation.Vertical" parent="">
<item name="android:activityOpenEnterAnimation">@anim/enter_from_bottom</item>
<item name="android:activityOpenExitAnimation">@anim/exit_to_bottom</item>
<item name="android:activityCloseEnterAnimation">@anim/enter_from_bottom</item>
<item name="android:activityCloseExitAnimation">@anim/exit_to_bottom</item>
<item name="android:windowEnterAnimation">@anim/enter_from_bottom</item>
<item name="android:windowExitAnimation">@anim/exit_to_bottom</item>
</style>
Also, for some reason this only worked from Android 8 and above. I added the following code to my BaseActivity, to fix it for the API levels below:
override fun finish() {
super.finish()
setAnimationsFix()
}
/**
* The activityCloseExitAnimation and activityCloseEnterAnimation properties do not work correctly when applied from the theme.
* So in this fix, we retrieve them from the theme, and apply them.
* @suppress Incorrect warning: https://stackoverflow.com/a/36263900/1395437
*/
@SuppressLint("ResourceType")
private fun setAnimationsFix() {
// Retrieve the animations set in the theme applied to this activity in the manifest..
var activityStyle = theme.obtainStyledAttributes(intArrayOf(attr.windowAnimationStyle))
val 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 = theme.obtainStyledAttributes(windowAnimationStyleResId, intArrayOf(activityCloseEnterAnimation, activityCloseExitAnimation))
val activityCloseEnterAnimation = activityStyle.getResourceId(0, 0)
val activityCloseExitAnimation = activityStyle.getResourceId(1, 0)
activityStyle.recycle()
overridePendingTransition(activityCloseEnterAnimation, activityCloseExitAnimation);
}