В Android, если вы хотите передать аргументы классу фрагмента, рекомендуется использовать фабричный метод.
class CallFragment : BaseFragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(R.layout.fragment_create, container, false)
// Get arguments' value
val title = arguments!!.getString(ARGUMENT_TITLE)
val description = arguments!!.getString(ARGUMENT_DESCRIPTION)
val location = arguments!!.getString(ARGUMENT_LOCATION)
val status = arguments!!.getString(ARGUMENT_STATUS)
return view
}
companion object {
private const val ARGUMENT_TITLE = "ARGUMENT_TITLE"
private const val ARGUMENT_DESCRIPTION = "ARGUMENT_DESCRIPTION"
private const val ARGUMENT_LOCATION = "ARGUMENT_LOCATION"
private const val ARGUMENT_STATUS = "ARGUMENT_STATUS"
/**
* Using this factory method to create an instance of this fragment based on given arguments.
*/
fun newInstance(title: String, description: String, location: String, status: String): CallFragment {
return CallFragment().apply {
val args = Bundle().apply {
putString(ARGUMENT_TITLE, title)
putString(ARGUMENT_DESCRIPTION, description)
putString(ARGUMENT_LOCATION, location)
putString(ARGUMENT_STATUS, status)
}
arguments = args
}
}
}
}
А в деятельности, которая использует ниже фрагмент
override fun onTabSelected(p0: TabLayout.Tab?) {
val position = p0?.position
when (position) {
0 ->
supportFragmentManager.beginTransaction().replace(
R.id.frame_container,
CallFragment.newInstance(mWorkOrder.title,mWorkOrder.description,mWorkOrder.location.name,mWorkOrder.status)
).addToBackStack(null).commit()
}
}