How to pass and get value from fragment and activity?
There is the companion object for that (https://kotlinlang.org/docs/reference/object-declarations.html#companion-objects )
Define your fragment as usual, and declare the companion that acts as the static newInstance() equivalent in Java :
class ViewStackListFragment : Fragment() {
companion object {
fun newInstance(position: Int): ViewStackListFragment {
val fragment = ViewStackListFragment()
val args = Bundle()
args.putInt("position", position)
fragment.setArguments(args)
return fragment
}
}
}
And simply call it like in Java :
val fragment = ViewStackListFragment.newInstance(4)