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:
parent
0028ab79c3
commit
d1518956e1
5 changed files with 72 additions and 50 deletions
|
|
@ -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
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue