Skip to content

Instantly share code, notes, and snippets.

@pandulapeter
Created April 18, 2019 06:56
Show Gist options
  • Save pandulapeter/0afcc72ffa5c18e68e7b6d4001c91169 to your computer and use it in GitHub Desktop.
Save pandulapeter/0afcc72ffa5c18e68e7b6d4001c91169 to your computer and use it in GitHub Desktop.
enum class TransitionType {
SIBLING, DETAIL, MODAL
}
inline fun <reified T : Fragment> FragmentManager.handleReplace(
tag: String = T::class.java.name,
addToBackStack: Boolean = false,
@IdRes containerId: Int = R.id.fragment_container,
transitionType: TransitionType? = null,
crossinline newInstance: () -> T
) {
beginTransaction().apply {
transitionType?.let { setTransition(it) }
replace(containerId, findFragmentByTag(tag) ?: newInstance.invoke(), tag)
if (addToBackStack) {
addToBackStack(null)
}
setReorderingAllowed(true)
commitAllowingStateLoss()
}
}
fun FragmentTransaction.setTransition(transitionType: TransitionType) {
setCustomAnimations(
when (transitionType) {
TransitionType.SIBLING -> R.anim.fade_in
TransitionType.DETAIL -> R.anim.slide_from_end
TransitionType.MODAL -> R.anim.slide_from_bottom
},
R.anim.fade_out,
R.anim.fade_in,
when (transitionType) {
TransitionType.SIBLING -> R.anim.fade_out
TransitionType.DETAIL -> R.anim.slide_to_end
TransitionType.MODAL -> R.anim.slide_to_bottom
}
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment