In your new Activity-class you create a similiar method like the runFadeAnimation I wrote and then you run it in onCreate and don't forget to change the resources id to fadeout.
我想使用 styles.xml 解决方案,但是它不适合我的活动。
Turns out that instead of using android:windowEnterAnimation and android:windowExitAnimation, I need to use the activity animations like this:
Also, for some reason this only worked from Android 8 and above.
我将下面的代码添加到我的 BaseActivity 中,以修复下面的 API 级别:
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(android.R.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(android.R.attr.activityCloseEnterAnimation, android.R.attr.activityCloseExitAnimation))
val activityCloseEnterAnimation = activityStyle.getResourceId(0, 0)
val activityCloseExitAnimation = activityStyle.getResourceId(1, 0)
activityStyle.recycle()
overridePendingTransition(activityCloseEnterAnimation, activityCloseExitAnimation);
}
Most of the answers are pretty correct, but some of them are deprecated such as when using R.anim.hold and some of them are just elaboratig the process.