Add backwards compatible implementation of overrideActivityTransition() (#4408)

This pull request adds `overrideActivityTransitionCompat()`, a
backwards-compatible version of `Activity.overrideActivityTransition()`
to be called in `Activity.onCreate()` in all Android versions.

This avoids duplicating the transition logic in different places of the
app to support older Android versions (in the activity launching code,
in `Activity.onCreate()` or in `Activity.finish()`).

- On API 34+, the implementation simply delegates to
`Activity.overrideActivityTransition()`.
- On API < 34, the implementation calls
`Activity.overridePendingTransition()` either immediately (opening
transition) or schedules it to be called later when the Activity is
finishing (closing transition).
- Rename `ActivityExensions.kt` to `ActivityExtensions.kt` (fix typo).
This commit is contained in:
Christophe Beyls 2024-05-10 13:59:35 +02:00 committed by GitHub
commit d1518956e1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 72 additions and 50 deletions

View file

@ -0,0 +1,50 @@
@file:JvmName("ActivityExtensions")
package com.keylesspalace.tusky.util
import android.app.Activity
import android.content.Intent
import android.os.Build
import androidx.activity.ComponentActivity
import androidx.annotation.AnimRes
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver
import com.keylesspalace.tusky.BaseActivity
fun Activity.startActivityWithSlideInAnimation(intent: Intent) {
// the new transition api needs to be called by the activity that is the result of the transition,
// so we pass a flag that BaseActivity will respect.
intent.putExtra(BaseActivity.OPEN_WITH_SLIDE_IN, true)
startActivity(intent)
}
/**
* Call this method in Activity.onCreate() to configure the open or close transitions.
*/
@Suppress("DEPRECATION")
fun ComponentActivity.overrideActivityTransitionCompat(
overrideType: Int,
@AnimRes enterAnim: Int,
@AnimRes exitAnim: Int
) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
overrideActivityTransition(overrideType, enterAnim, exitAnim)
} else {
if (overrideType == ActivityConstants.OVERRIDE_TRANSITION_OPEN) {
overridePendingTransition(enterAnim, exitAnim)
} else {
lifecycle.addObserver(
LifecycleEventObserver { _, event ->
if (event == Lifecycle.Event.ON_PAUSE && isFinishing) {
overridePendingTransition(enterAnim, exitAnim)
}
}
)
}
}
}
object ActivityConstants {
const val OVERRIDE_TRANSITION_OPEN = 0
const val OVERRIDE_TRANSITION_CLOSE = 1
}