skydoves / Bindables

🧬 Android DataBinding kit that enables notifying UI layers of data changes without requiring backing properties or relying on reactive programming models like LiveData and StateFlow.
Apache License 2.0
322 stars 19 forks source link

Support for using onCreateDialog in a DialogFragment #10

Closed epkjoja closed 3 years ago

epkjoja commented 3 years ago

Is your feature request related to a problem?

I started to use your library in my project and it works really great, except for one thing. My DialogFragments are using onCreateDialog and an AlertDialogBuilder to inflate the layout. When trying to use the binding at that point it will result in an IllegalStateException with message "...binding cannot be accessed before onCreateView() or after onDestroyView()". Would it be possible to make your BindingDialogFragment to also support this use case in some way?

skydoves commented 3 years ago

Hi @epkjoja, Could you check that called super.onCreateView(inflater, container, savedInstanceState) on your onCreateView method? And the binding property should not be accessed onCreateView() or after onDestroyView(), because it aligns with the fragment's lifecycle.

epkjoja commented 3 years ago

My DialogFrament's doesn't override onCreateView at all. They override onCreateDialog instead. See the documentation for onCreateDialog here: https://developer.android.com/reference/androidx/fragment/app/DialogFragment#onCreateDialog(android.os.Bundle)

As the documentation states, onCreateDialog will be called just before onCreateView and you don't have to override onCreateView at all. So it's clear that I can't use your library as it is today because it won't allow usage before onCreateView.

I don't know if it's at all possible to support this use case with your library? It might be so that such a use case isn't supported by data binding?

skydoves commented 3 years ago

Hi, @epkjoja Could you check using the 1.0.9-SNAPSHOT? On your DialogFragment, you should call super.onCreateDialog before using the binding property. Thanks!

epkjoja commented 3 years ago

Hi @skydoves, No, it's still the same error with the new version. _binding is still uninitialized when I try to fetch it from my onCreateDialog.

skydoves commented 3 years ago

Hi, sorry for the late reply. I think if you are using the BindableDialogFragment for implementing just an AlertDialog, I do not recommend using the way. The purpose of the Bindable___ is to inflate the basic databinding property for communicating view models. So every view inflation should be done in the onCreateView and the binding property must be used in the onViewCreated. If you need to handle both ways, inflating the custom view for the AlertDialog must be handled independently regardless of the BindableDialogFragment's process.