Skip to content

Instantly share code, notes, and snippets.

@NsAveek

NsAveek/InlineFunctions.kt

Last active Nov 14, 2019
Embed
What would you like to do?
const val addFragment = 1
const val replaceFragment = 2
private inline fun generateFragmentTransaction(fragmentToReplaceOrAdd : Fragment, addOrReplace: Int) : FragmentTransaction {
return when(addOrReplace) {
addFragment -> supportFragmentManager.beginTransaction().add(R.id.container_frame, fragmentToReplaceOrAdd)
else -> supportFragmentManager.beginTransaction().replace(R.id.container_frame, fragmentToReplaceOrAdd)
}
}
private fun fragmentAddOrReplacer(name : String, transaction : ()-> FragmentTransaction){
transaction().apply {
addToBackStack(name).commit()
}
}
private fun openFragmentA() {
fragmentAddOrReplacer("sampleFragment") { generateFragmentTransaction(FragmentA.newInstance(), replaceFragment) }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment