https://kotlinlang.org logo
#android
Title
# android
r

rodrigo

11/26/2019, 8:52 PM
I am facing a weird issue with Kotlin synthetic on Android, at
onActivityCreated
I have a field
reference_field.addTextChangedListener()
and then I navigate to a different screen (Fragment), however when I navigate back the listener no longer works and I think it has something to do with how Kotlin Android Extensions use cached views, on the other hand if I replace
reference_field.addTextChangedListener()
with
view?.findViewById<EditText>(R.id.reference_field)?.addTextChangedListener()
then navigate to a different screen and return to the previous screen the listener does work as intended. Perhaps when the view gets destroyed and recreated Kotlin synthetic is referring to a cached reference which no longer exists 🤔. Has anyone experienced this before?
a

Amirul Zin

11/27/2019, 2:35 AM
Hence why I avoid Kotlin synthetics like the plague when I see that it’s using a View cache. That’s like hot mess of bugs waiting to happen on unsuspecting devs. You don’t want to mess with a cache of Android Views especially with its own mess of lifecycle which you (and even the Kotlin Synthetics library itself) can’t fully control. Just use ViewBinding or the view id generation part of DataBinding salivate at the proper typed null-aware O(1) access, and call it a day.
s

stantronic

11/28/2019, 9:20 AM
That’s interesting @Amirul Zin Do you know of any articles on this? We’ve got kotlin synthetics all over our codebases, and am hoping to persuade my team to move toward data-binding, so anything that can help inform the team would be great.
4 Views