Closed oguzzhan closed 10 months ago
This is my first time using data binding, but here are my thoughts.
I know you didn't say it, but there is always someone saying to never pass Views around due to potential memory leaks. It is my understanding that you can couple the xml and Fragment without having to worry about a memory leak because when the Fragment being referenced has its view destroyed, it nulls out the binding.
There is the use case of when you want to reuse the xml in different fragments to allow for different behaviors, and in this case, it may be good to have an abstract Fragment base or an interface, and use that in the xml, so then you have an explicit declaration of the interface that is expected from the users of the xml.
One disadvantage would be that you cannot breakpoint debug xml, but that can be fixed by calling a single method from the xml and debugging the function called. Another being that although the functions being directly in the xml can make understanding the layout file easier, comments would be needed in the Fragment to know where is functions are being used without having to jump around the two files.
The use case in this codelab appears to access the function of the Fragment in the xml to clear clutter from the class file, and to show off the capabilities of data binding.
To me, it seems that keeping a Fragment reference in the xml can be advantageous, but the use case here seems to skip over the advantageous use cases, and may even be slightly disadvantageous as the UI logic is now contained in two files instead of one.
URL of codelab https://developer.android.com/codelabs/basic-android-kotlin-training-shared-viewmodel#8
In which task and step of the codelab can this issue be found? Task 9
Describe the problem Is passing Fragment to the binding layout the best practice ?