There is a scenario where a DialogFragment is dismissed after being shown but before onViewCreated is called. In this case, onDestroyView will be invoked, but the binding has not been initialized yet. Accessing the binding at this point calls requireView(), which leads to a java.lang.IllegalStateException crash. Besides manually checking whether the view is null in onDestroyView, is there a better way to handle this situation?
class MyDialogFragment {
private val binding by viewBinding(MyDialogFragmentBinding::bind)
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.myView.text = "sample"
}
override fun onDestroyView() {
super.onDestroyView()
binding.myView.reset()
}
}