mirror of https://github.com/tuskyapp/Tusky.git
Browse Source
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).pull/4420/head
5 changed files with 72 additions and 50 deletions
@ -1,25 +0,0 @@
|
||||
@file:JvmName("ActivityExtensions") |
||||
|
||||
package com.keylesspalace.tusky.util |
||||
|
||||
import android.app.Activity |
||||
import android.content.Intent |
||||
import android.os.Build |
||||
import com.keylesspalace.tusky.BaseActivity |
||||
import com.keylesspalace.tusky.R |
||||
|
||||
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) |
||||
if (!supportsOverridingActivityTransitions()) { |
||||
// the old api needs to be called by the activity that starts the transition |
||||
@Suppress("DEPRECATION") |
||||
overridePendingTransition(R.anim.activity_open_enter, R.anim.activity_open_exit) |
||||
} |
||||
} |
||||
|
||||
fun supportsOverridingActivityTransitions(): Boolean { |
||||
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE |
||||
} |
||||
@ -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…
Reference in new issue