Skip to content

Commit

Permalink
fix ActivityUtils.overrideActivityTransition() to be able to be calle…
Browse files Browse the repository at this point in the history
…d from Activity.onCreate()
  • Loading branch information
cbeyls committed May 3, 2024
1 parent 6896ce7 commit 688ec12
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ class SearchResultActivity : AppCompatActivity(R.layout.search_result) {
R.anim.fade_in,
R.anim.fade_out
)
ActivityUtils.overrideActivityTransition(
this,
ActivityUtils.OVERRIDE_TRANSITION_CLOSE,
R.anim.fade_in,
R.anim.fade_out
)
setSupportActionBar(findViewById(R.id.toolbar))

supportActionBar?.setDisplayHomeAsUpEnabled(true)
Expand Down Expand Up @@ -97,16 +103,6 @@ class SearchResultActivity : AppCompatActivity(R.layout.search_result) {
return true
}

override fun finish() {
super.finish()
ActivityUtils.overrideActivityTransition(
this,
ActivityUtils.OVERRIDE_TRANSITION_CLOSE,
R.anim.fade_in,
R.anim.fade_out
)
}

private val EditText.textChangeEvents: Flow<CharSequence?>
get() = callbackFlow {
val textWatcher = doOnTextChanged { text, _, _, _ -> trySend(text) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ class SettingsActivity : SimpleToolbarActivity() {
R.anim.slide_in_right,
R.anim.partial_zoom_out
)
ActivityUtils.overrideActivityTransition(
this,
ActivityUtils.OVERRIDE_TRANSITION_CLOSE,
R.anim.partial_zoom_in,
R.anim.slide_out_right
)

supportActionBar?.setDisplayHomeAsUpEnabled(true)

Expand All @@ -30,14 +36,4 @@ class SettingsActivity : SimpleToolbarActivity() {
onBackPressedDispatcher.onBackPressed()
return true
}

override fun finish() {
super.finish()
ActivityUtils.overrideActivityTransition(
this,
ActivityUtils.OVERRIDE_TRANSITION_CLOSE,
R.anim.partial_zoom_in,
R.anim.slide_out_right
)
}
}
19 changes: 16 additions & 3 deletions app/src/main/java/be/digitalia/fosdem/utils/ActivityUtils.kt
Original file line number Diff line number Diff line change
@@ -1,24 +1,37 @@
package be.digitalia.fosdem.utils

import android.app.Activity
import android.os.Build
import androidx.activity.ComponentActivity
import androidx.annotation.AnimRes
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver

object ActivityUtils {
const val OVERRIDE_TRANSITION_OPEN = 0
const val OVERRIDE_TRANSITION_CLOSE = 1

/**
* Call this method in Activity.onCreate() to configure the open or close transitions.
*/
@Suppress("DEPRECATION")
fun overrideActivityTransition(
activity: Activity,
activity: ComponentActivity,
overrideType: Int,
@AnimRes enterAnim: Int,
@AnimRes exitAnim: Int
) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
activity.overrideActivityTransition(overrideType, enterAnim, exitAnim)
} else {
activity.overridePendingTransition(enterAnim, exitAnim)
if (overrideType == OVERRIDE_TRANSITION_OPEN) {
activity.overridePendingTransition(enterAnim, exitAnim)
} else {
activity.lifecycle.addObserver(LifecycleEventObserver { _, event ->
if (event == Lifecycle.Event.ON_PAUSE && activity.isFinishing) {
activity.overridePendingTransition(enterAnim, exitAnim)
}
})
}
}
}
}

0 comments on commit 688ec12

Please sign in to comment.