Skip to content

java.lang.IllegalStateException when accessing binding with requireView() in DialogFragment’s onDestroyView #130

@Keeeya

Description

@Keeeya

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()
    }
}

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions