I have started to study Kotlin and do not know all the functionality of the language.
The function is used to show the fragment in the FrameLayout. The logic is so that the first time it should always add()
the fragment and next times it will replace()
. Also in some cases I need to use addToBackStack()
and also in the same situations to disable the left-side menu.
fun showFragment(fragment : Fragment,
isReplace: Boolean = true,
backStackTag: String? = null,
isEnabled: Boolean = true)
{
/* Defining fragment transaction */
val fragmentTransaction = supportFragmentManager
.beginTransaction()
/* Select if to replace or add a fragment */
if(isReplace)
fragmentTransaction.replace(R.id.frameLayoutContent, fragment, backStackTag)
else
fragmentTransaction.add(R.id.frameLayoutContent, fragment)
/* Select if to add to back stack */
if(backStackTag != null)
fragmentTransaction.addToBackStack(fragment.javaClass.name)
fragmentTransaction.commit()
enableDrawer(isEnabled)
}
Question: Are there some possible improvements of the function code related to the specifications of Kotlin language to make code cleaner as for now the function looks as a mass.
I have posted a blog about the below answer.
I will write an Extension function
to the FragmentManager
which accepts a Lambda with Receiver
as an argument.
inline fun FragmentManager.inTransaction(func: FragmentTransaction.() -> Unit) {
val fragmentTransaction = beginTransaction()
fragmentTransaction.func()
fragmentTransaction.commit()
}
To add a fragment
, we can call like this from the Activity
now:
supportFragmentManager.inTransaction {
add(R.id.frameLayoutContent, fragment)
}
The advantage of this is we don't have to call beginTransaction()
and commit()
every time we add or replace a Fragment
now. How many hours have you wasted debugging only to find out that you have missed calling commit()
in Java?
Next, I will write Extension functions
to AppCompatActivity
:
fun AppCompatActivity.addFragment(fragment: Fragment, frameId: Int, backStackTag: String? = null) {
supportFragmentManager.inTransaction {
add(frameId, fragment)
backStackTag?.let { addToBackStack(fragment.javaClass.name) }
}
}
fun AppCompatActivity.replaceFragment(fragment: Fragment, frameId: Int, backStackTag: String? = null) {
supportFragmentManager.inTransaction {
replace(frameId, fragment)
backStackTag?.let { addToBackStack(fragment.javaClass.name) }
}
}
So that now we can add/ replace Fragment
from any Activity
in a single line, without any additional qualifiers:
addFragment(yourFragment, R.id.frameLayoutContent, "tag")
replaceFragment(yourFragment, R.id.frameLayoutContent, "tag")
I like to use with and let when I have some code related to using a lot some variable and null checks
So I'd do something like this:
fun showFragment(fragment : Fragment,
isReplace: Boolean = true,
backStackTag: String? = null,
isEnabled: Boolean = true)
{
/* Defining fragment transaction */
with(supportFragmentManager.beginTransaction()) {
/* Select if to replace or add a fragment */
if(isReplace)
replace(R.id.frameLayoutContent, fragment, backStackTag)
else
add(R.id.frameLayoutContent, fragment)
/* Select if to add to back stack */
backStackTag?.let { addToBackStack(it) }
commit()
}
enableDrawer(isEnabled)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With