Fragment Transactions with Kotlin

Published: August 11, 2019

First, add a helper method on the FragmentManager

kotlin
inline fun FragmentManager.inTransaction(func: FragmentTransaction.() -> FragmentTransaction) {
  beginTransaction().addToBackStack(null).func().commit()
}

Then we can call it, like this:

kotlin
supportFragmentManager.inTransaction {
  add(android.R.id.content, GalleryFragment())
}