2 years ago

#30495

test-img

Elye

Is Google Document proposed way of Fragment Creation conflicting with ViewBinding of Fragment?

In Google Documentation, we are guided to create Fragment using code as below, where we can pass the layout id through the constructor

class ExampleFragment : Fragment(R.layout.example_fragment)

At the same time, we know that we are recommended to use ViewBinding to access the view components. As per the Google Document

private var _binding: ResultProfileBinding? = null
// This property is only valid between onCreateView and
// onDestroyView.
private val binding get() = _binding!!

override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    _binding = ResultProfileBinding.inflate(inflater, container, false)
    val view = binding.root
    return view
}

override fun onDestroyView() {
    super.onDestroyView()
    _binding = null
}

It feels both will not work together. If one uses ViewBinding as per the Google Document, one will not able to use the layout ID through constructor as proposed by the Google Documentation.

Hence asking here to know if there's a way to get them to work together?

android

fragment

android-viewbinding

0 Answers

Your Answer

Accepted video resources