How to pass and get value from fragment and activity?
Do it in more Kotlin style
1) Create an inline function:
inline fun FRAGMENT.putArgs(argsBuilder: Bundle.() -> Unit): FRAGMENT = this.apply { arguments = Bundle().apply(argsBuilder) }
2) Now you can use this extension in all fragments without duplication of code:
class MyFragment: Fragment() {
companion object {
fun newInstance(name: String) = MyFragment().putArgs {
putString("nameKey", name)
}
}
}
class MyFragment1: Fragment() {
companion object {
fun newInstance(boolean: Boolean) = MyFragment1().putArgs {
putBoolean("booleanKey", boolean)
}
}
}
3) Create your fragments:
val myFragment = MyFragment.newInstance("NAME")
val myFragment1 = MyFragment1.newInstance(true)