Faraz
01/22/2018, 10:29 AMkotlin-android-extensions
for a while now and was very happy with it because it reduced a lot of work for me as a developer. But today i’ve come to a realization that although this tool is amazing, it can be abused or used in a very wrong way which you would’ve avoided when importing views the usual way findViewById()
.
an example case : you’ve an activity A
which contains 3 fragments AA
AB
and AC
, now if you want to access the views inside AA
,AB
or AC
from Activity A
(for some reason) the best way would’ve been to add a callback to fragment to access it’s view to avoid any kind of leak and this would make sure that fragment is inflated before you can access it’s view from it’s parent activity. but here’s where it gets interesting when using synthetics, since there’s no check when importing you can import all the views of all the fragments inside parent activity A
, and the IDE will not even warn you . so, you can access all the views of fragments AA
AB
and AC
ignoring the fact that they might not have been inflated inside the fragment at the time of calling.
this
1. may make developers pick up a bad practice especially for beginners
2. may make developers lazy from doing there job properly and avoid a potential bug in their app
BUT it’s all upto to us developers how well we maintain our code so this depends on individual, so is there anything else that i or any other developers who are using kotlin-android-extensions
need to know if we should use it or not, if not why? and what could be other alternatives for it? apart from that is there any performance costs hidden behind this extension which we could’ve avoided using findViewById()