Would it be there noticeable speed improvement if I would create local references to needed view elements (e.g. EditText
or Button
) in my activity (in onCreate()
) and use them for accessing needed elements or it does not much matter if I always use findViewById()
when I need to access some particular element?
views:
44answers:
2
+1
A:
It will be a speed improvement if you have a complex layout and you are accessing those View
s too often. It is a good practice to define private variables and to bind them to the references returned by findViewById
once in onCreate()
then accessing them throughout your code.
If you are accessing those Views just once for, let's say, adding OnClickListeners to them, I don't think it is needed to create local references, as you call them.
licorna
2010-06-17 18:40:07
+2
A:
If you are using a ListActivity this is what the View Holder pattern advocates. I would say it depends on how often the view is getting refreshed.
Here's a video and pdf from Google I/O which talks about implementing a ViewHolder pattern in the getView method of a ListAdapter
mmaitlen
2010-06-17 18:41:45