Created
April 18, 2019 06:56
-
-
Save pandulapeter/0afcc72ffa5c18e68e7b6d4001c91169 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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